[ https://issues.apache.org/jira/browse/LOG4J2-2981?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17292589#comment-17292589 ]
ASF subversion and git services commented on LOG4J2-2981: --------------------------------------------------------- Commit b137f443872c07336080f6d6b41b03a5ed915f26 in logging-log4j2's branch refs/heads/master from Ralph Goers [ https://gitbox.apache.org/repos/asf?p=logging-log4j2.git;h=b137f44 ] LOG4J2-2981 - OnStartupTriggeringPolicy would fail to cause the file to roll over with DirectWriteTriggeringPolicy unless minSize was set to 0 > OnStartupTriggeringPolicy only rolls over when minsize = 0 with > DirectWriteTriggeringPolicy > ------------------------------------------------------------------------------------------- > > 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 > Assignee: Ralph Goers > Priority: Minor > Labels: Policy, RollingFileAppender > > 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)