哪个方法在log4j2中实际执行翻转

Which method actually performs rollover in log4j2

我想知道哪个方法实际执行 log4j2 中的文件翻转。我试着在源代码中搜索它,但没有找到。

DefaultRolloverStrategy class 中有一个方法 public RolloverDescription rollover(final RollingFileManager manager),我在 CustomStrategy class 中覆盖了它,结果发现这个方法不执行实际翻转。

谁能帮我解决这个问题?

你要找的实际上是RolloverDescription,它包含两种类型的动作:

AsyncAction - Action to be completed after close of current active log file and before next rollover attempt, may be executed asynchronously.

SyncAction - Action to be completed after close of current active log file before returning control to caller.

执行这些操作的部分是 RollingFileManager.rollover

        final RolloverDescription descriptor = strategy.rollover(this);
        if (descriptor != null) {
            writeFooter();
            closeOutputStream();
            if (descriptor.getSynchronous() != null) {
                LOGGER.debug("RollingFileManager executing synchronous {}", descriptor.getSynchronous());
                try {
                    success = descriptor.getSynchronous().execute();
                } catch (final Exception ex) {
                    success = false;
                    logError("Caught error in synchronous task", ex);
                }
            }

            if (success && descriptor.getAsynchronous() != null) {
                LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
                asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
                releaseRequired = false;
            }
            return true;
        }

所以您的 CustomStrategy.rollover 就是上面代码片段第一行中发生的事情。 CustomStrategy.rollover returns RolloverDescription,它包含要执行的操作,正如我在上面解释的那样。