Samuel Martin created LOG4J2-2981:
-------------------------------------
Summary: OnStartupTriggeringPolicy only rolls over when minsize = 0
Key: LOG4J2-2981
URL: https://issues.apache.org/jira/browse/LOG4J2-2981
Project: Log4j 2
Issue Type: Bug
Components: Appenders
Affects Versions: 2.14.0
Reporter: Samuel Martin
I am trying to use a {{RollingFileAppender}} with a
{{CompositeTriggeringPolicy}} that includes a {{OnStartupTriggeringPolicy}}
which is deemed to rollover the log when the JVM is (re-)started.
However, I have noted that the files only roll over on startup when I specify
"minsize = 0" on the log4j2.xml configuration file. If I put any other value or
leave the minsize unspecified (i.e. rely on its default value minsize = 1), the
appender proceeds writing to the previously existing file.
I have run the debugger and traced down the bug according as follows.
All the logic in {{OnStartupTriggeringPolicy.initialize()}} is conditioned upon
the size of the rolling file being larger than {{minSize}}
{code:java}
public void initialize(final RollingFileManager manager) {
if (manager.getFileTime() < JVM_START_TIME && manager.getFileSize() >=
minSize) {
...
}
}
{code}
However, {{manager.getFileSize()}} always returns 0, thus the body of that if
is never entered (... unless {{minSize == 0}}). In fact, this call to
{{OnStartupTriggeringPolicy.initialize()}} is invoked indeed from
{{RollingFileManager.initialize()}} *before* any value has been assigned to
{{size}}
{code:java}
public void initialize() {
if (!initialized) {
LOGGER.debug("Initializing triggering policy {}", triggeringPolicy);
initialized = true;
triggeringPolicy.initialize(this);
if (triggeringPolicy instanceof LifeCycle) {
((LifeCycle) triggeringPolicy).start();
}
if (directWrite) {
// LOG4J2-2485: Initialize size from the most recently written
file.
File file = new File(getFileName());
if (file.exists()) {
size = file.length();
} else {
((DirectFileRolloverStrategy)
rolloverStrategy).clearCurrentFileName();
}
}
}
}
{code}
Thus, within {{OnStartupTriggeringPolicy.initialize()}},
{{manager.getFileSize()}} always evaluates to {{0}}, and the only way that the
file is rolled over at startup is that {{minSize}} has been configured as {{0}}.
As a side consequence, this may also affect SizeBaseTriggerinPolicy if a
rollover is needed by coincidence during startup.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)