如何清除 Log4j 2.3 每日滚动文件附加器的日志

How to clear log for Log4j 2.3 daily rolling file appender

我目前正在使用 log4j2 为我的 Web 应用程序编写日志。我已经配置了一个滚动文件附加器以在每晚午夜滚动。

我的log4j2.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %c{36} - %msg%n"/>
        </Console>
        <RollingFile
            name="ApplicationLog"
            fileName="${sys:catalina.base}/logs/application.log"
            filePattern="${sys:catalina.base}/logs/application %d{yyyy-MM-dd}.log">
              <PatternLayout>
                  <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %c{36} %l: %msg%n</Pattern>
              </PatternLayout>
              <Policies>
                  <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
              </Policies>
              <DefaultRolloverStrategy/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="my.package" level="info" additivity="false">
            <AppenderRef ref="ApplicationLog"/>
        </Logger>
        <Logger name="my.test.package" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

目前,这会写入名为 application.log 的日志,然后在午夜将当前 application.log 文件复制到应用程序 yyyy-MM-dd.log(文件名中的日期被正确替换),并在第二天 application.log.

中继续记录

我希望在开始新的一天之前清除 application.log。目前,这还没有发生,因此我的日志大小不断增加。我的第 1 天日志包含第 1 天的所有日志记录,但我的第 2 天日志包含第 1 天和第 2 天的日志记录,依此类推。

翻转发生后如何重置 application.log 文件?根据 Log4j 2.x documentation,应该创建一个新的 application.log 文件,而不是继续当前文件,但这不是我遇到的行为。

如有任何帮助,我们将不胜感激。

谢谢, 亚当

EDIT:这是下面 rajuGT 请求的配置跟踪。我看不出这里有什么问题...

2015-09-23 08:06:29,931 DEBUG Starting configuration XmlConfiguration[location=C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml]
2015-09-23 08:06:29,931 DEBUG PluginManager 'Core' found 69 plugins
2015-09-23 08:06:29,932 DEBUG PluginManager 'Level' found 0 plugins
2015-09-23 08:06:29,934 DEBUG PluginManager 'Lookup' found 10 plugins
2015-09-23 08:06:29,939 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. Searching for builder factory method...
2015-09-23 08:06:29,944 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.layout.PatternLayout$Builder org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
2015-09-23 08:06:29,962 DEBUG TypeConverterRegistry initializing.
2015-09-23 08:06:29,963 DEBUG PluginManager 'TypeConverter' found 21 plugins
2015-09-23 08:06:29,979 DEBUG Calling build() on class class org.apache.logging.log4j.core.layout.PatternLayout$Builder for element PatternLayout with params(name="%d{HH:mm:ss.SSS} [%t] %-5level %c{36} - %msg%n", Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml), null, name="null", name="null", name="null", name="null", name="null")
2015-09-23 08:06:29,980 DEBUG PluginManager 'Converter' found 33 plugins
2015-09-23 08:06:29,981 DEBUG Built Plugin[name=layout] OK from builder factory method.
2015-09-23 08:06:29,981 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender]. Searching for builder factory method...
2015-09-23 08:06:29,984 INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2015-09-23 08:06:29,985 DEBUG PluginManager 'Converter' found 33 plugins
2015-09-23 08:06:29,987 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2015-09-23 08:06:29,987 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.appender.ConsoleAppender$Builder org.apache.logging.log4j.core.appender.ConsoleAppender.newBuilder().
2015-09-23 08:06:29,994 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-09-23 08:06:29,994 DEBUG Encountered type [org.apache.logging.log4j.core.layout.PatternLayout] which can only be checked for null.
2015-09-23 08:06:29,998 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-09-23 08:06:29,998 DEBUG Encountered type [org.apache.logging.log4j.core.appender.ConsoleAppender$Target] which can only be checked for null.
2015-09-23 08:06:29,999 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-09-23 08:06:29,999 DEBUG Calling build() on class class org.apache.logging.log4j.core.appender.ConsoleAppender$Builder for element Console with params(PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %c{36} - %msg%n), null, name="SYSTEM_OUT", name="Console", name="null", name="null")
2015-09-23 08:06:30,000 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2015-09-23 08:06:30,001 DEBUG Built Plugin[name=appender] OK from builder factory method.
2015-09-23 08:06:30,001 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. Searching for builder factory method...
2015-09-23 08:06:30,002 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.layout.PatternLayout$Builder org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
2015-09-23 08:06:30,003 DEBUG Calling build() on class class org.apache.logging.log4j.core.layout.PatternLayout$Builder for element PatternLayout with params(name="%d{HH:mm:ss.SSS} [%t] %-5level %c{36} %l: %msg%n", Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml), null, name="null", name="null", name="null", name="null", name="null")
2015-09-23 08:06:30,004 DEBUG Built Plugin[name=layout] OK from builder factory method.
2015-09-23 08:06:30,004 DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy]. Searching for builder factory method...
2015-09-23 08:06:30,009 DEBUG No builder factory method found in class org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy. Going to try finding a factory method instead.
2015-09-23 08:06:30,009 DEBUG Still building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy]. Searching for factory method...
2015-09-23 08:06:30,010 DEBUG Found factory method [createPolicy]: public static org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.createPolicy(java.lang.String,java.lang.String).
2015-09-23 08:06:30,016 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy for element TimeBasedTriggeringPolicy with params(name="1", name="true")
2015-09-23 08:06:30,018 DEBUG Built Plugin[name=TimeBasedTriggeringPolicy] OK from factory method.
2015-09-23 08:06:30,018 DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy]. Searching for builder factory method...
2015-09-23 08:06:30,019 DEBUG No builder factory method found in class org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy. Going to try finding a factory method instead.
2015-09-23 08:06:30,019 DEBUG Still building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy]. Searching for factory method...
2015-09-23 08:06:30,019 DEBUG Found factory method [createPolicy]: public static org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy.createPolicy(org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy[]).
2015-09-23 08:06:30,020 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy for element Policies with params(={TimeBasedTriggeringPolicy})
2015-09-23 08:06:30,020 DEBUG Built Plugin[name=Policies] OK from factory method.
2015-09-23 08:06:30,021 DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy]. Searching for builder factory method...
2015-09-23 08:06:30,023 DEBUG No builder factory method found in class org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy. Going to try finding a factory method instead.
2015-09-23 08:06:30,023 DEBUG Still building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy]. Searching for factory method...
2015-09-23 08:06:30,024 DEBUG Found factory method [createStrategy]: public static org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.createStrategy(java.lang.String,java.lang.String,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.Configuration).
2015-09-23 08:06:30,024 DEBUG Calling createStrategy on class org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy for element DefaultRolloverStrategy with params(name="null", name="null", name="null", name="null", Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml))
2015-09-23 08:06:30,025 DEBUG Built Plugin[name=DefaultRolloverStrategy] OK from factory method.
2015-09-23 08:06:30,025 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender]. Searching for builder factory method...
2015-09-23 08:06:30,026 DEBUG No builder factory method found in class org.apache.logging.log4j.core.appender.RollingFileAppender. Going to try finding a factory method instead.
2015-09-23 08:06:30,026 DEBUG Still building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender]. Searching for factory method...
2015-09-23 08:06:30,026 DEBUG Found factory method [createAppender]: public static org.apache.logging.log4j.core.appender.RollingFileAppender org.apache.logging.log4j.core.appender.RollingFileAppender.createAppender(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy,org.apache.logging.log4j.core.appender.rolling.RolloverStrategy,org.apache.logging.log4j.core.Layout,org.apache.logging.log4j.core.Filter,java.lang.String,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.Configuration).
2015-09-23 08:06:30,029 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile with params(name="C:\apache-tomcat-8.0.23/logs/application.log", name="C:\apache-tomcat-8.0.23/logs/application %d{yyyy-MM-dd}.log", name="null", name="ApplicationLog", name="null", name="null", name="null", Policies(CompositeTriggeringPolicy{TimeBasedTriggeringPolicy}), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=7)), PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %c{36} %l: %msg%n), null, name="null", name="null", name="null", Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml))
2015-09-23 08:06:30,041 DEBUG Starting RollingFileManager C:\apache-tomcat-8.0.23/logs/application.log
2015-09-23 08:06:30,044 DEBUG PluginManager 'FileConverter' found 2 plugins
2015-09-23 08:06:30,049 TRACE PatternProcessor.getNextTime returning 2015/09/24-00:00:00.000, nextFileTime=2015/09/23-00:00:00.000, prevFileTime=1970/01/01-10:00:00.000, current=2015/09/23-08:06:30.048, freq=DAILY
2015-09-23 08:06:30,049 TRACE PatternProcessor.getNextTime returning 2015/09/24-00:00:00.000, nextFileTime=2015/09/23-00:00:00.000, prevFileTime=2015/09/23-00:00:00.000, current=2015/09/23-08:06:30.049, freq=DAILY
2015-09-23 08:06:30,050 DEBUG Built Plugin[name=appender] OK from factory method.
2015-09-23 08:06:30,050 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for builder factory method...
2015-09-23 08:06:30,051 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppendersPlugin. Going to try finding a factory method instead.
2015-09-23 08:06:30,051 DEBUG Still building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for factory method...
2015-09-23 08:06:30,051 DEBUG Found factory method [createAppenders]: public static java.util.concurrent.ConcurrentMap org.apache.logging.log4j.core.config.AppendersPlugin.createAppenders(org.apache.logging.log4j.core.Appender[]).
2015-09-23 08:06:30,052 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders with params(={Console, ApplicationLog})
2015-09-23 08:06:30,052 DEBUG Built Plugin[name=appenders] OK from factory method.
2015-09-23 08:06:30,052 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for builder factory method...
2015-09-23 08:06:30,053 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppenderRef. Going to try finding a factory method instead.
2015-09-23 08:06:30,053 DEBUG Still building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for factory method...
2015-09-23 08:06:30,053 DEBUG Found factory method [createAppenderRef]: public static org.apache.logging.log4j.core.config.AppenderRef org.apache.logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang.String,org.apache.logging.log4j.Level,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,054 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(name="ApplicationLog", name="null", null)
2015-09-23 08:06:30,054 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2015-09-23 08:06:30,055 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for builder factory method...
2015-09-23 08:06:30,055 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig. Going to try finding a factory method instead.
2015-09-23 08:06:30,056 DEBUG Still building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for factory method...
2015-09-23 08:06:30,056 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,057 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(name="false", name="INFO", name="my.package", name="null", ={ApplicationLog}, ={}, Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml), null)
2015-09-23 08:06:30,059 DEBUG Built Plugin[name=logger] OK from factory method.
2015-09-23 08:06:30,059 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for builder factory method...
2015-09-23 08:06:30,059 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppenderRef. Going to try finding a factory method instead.
2015-09-23 08:06:30,060 DEBUG Still building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for factory method...
2015-09-23 08:06:30,060 DEBUG Found factory method [createAppenderRef]: public static org.apache.logging.log4j.core.config.AppenderRef org.apache.logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang.String,org.apache.logging.log4j.Level,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,061 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(name="Console", name="null", null)
2015-09-23 08:06:30,061 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2015-09-23 08:06:30,061 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for builder factory method...
2015-09-23 08:06:30,061 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig. Going to try finding a factory method instead.
2015-09-23 08:06:30,062 DEBUG Still building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for factory method...
2015-09-23 08:06:30,062 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,064 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(name="false", name="DEBUG", name="my.test.package", name="null", ={Console}, ={}, Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml), null)
2015-09-23 08:06:30,064 DEBUG Built Plugin[name=logger] OK from factory method.
2015-09-23 08:06:30,065 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for builder factory method...
2015-09-23 08:06:30,065 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppenderRef. Going to try finding a factory method instead.
2015-09-23 08:06:30,065 DEBUG Still building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for factory method...
2015-09-23 08:06:30,066 DEBUG Found factory method [createAppenderRef]: public static org.apache.logging.log4j.core.config.AppenderRef org.apache.logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang.String,org.apache.logging.log4j.Level,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,066 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(name="Console", name="null", null)
2015-09-23 08:06:30,066 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2015-09-23 08:06:30,067 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for builder factory method...
2015-09-23 08:06:30,067 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger. Going to try finding a factory method instead.
2015-09-23 08:06:30,067 DEBUG Still building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for factory method...
2015-09-23 08:06:30,068 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig$RootLogger.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2015-09-23 08:06:30,068 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(name="null", name="ERROR", name="null", ={Console}, ={}, Configuration(C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml), null)
2015-09-23 08:06:30,069 DEBUG Built Plugin[name=root] OK from factory method.
2015-09-23 08:06:30,069 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for builder factory method...
2015-09-23 08:06:30,071 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggersPlugin. Going to try finding a factory method instead.
2015-09-23 08:06:30,071 DEBUG Still building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for factory method...
2015-09-23 08:06:30,071 DEBUG Found factory method [createLoggers]: public static org.apache.logging.log4j.core.config.Loggers org.apache.logging.log4j.core.config.LoggersPlugin.createLoggers(org.apache.logging.log4j.core.config.LoggerConfig[]).
2015-09-23 08:06:30,072 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element Loggers with params(={my.package, my.test.package, root})
2015-09-23 08:06:30,072 DEBUG Built Plugin[name=loggers] OK from factory method.
2015-09-23 08:06:30,074 DEBUG Started configuration XmlConfiguration[location=C:\apache-tomcat-8.0.23\webapps\myApp\WEB-INF\classes\log4j2.xml] OK.
2015-09-23 08:06:30,075 TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@43db0439...
2015-09-23 08:06:30,075 TRACE AbstractConfiguration stopped 0 AsyncLoggerConfigs.
2015-09-23 08:06:30,075 TRACE AbstractConfiguration stopped 0 AsyncAppenders.
2015-09-23 08:06:30,075 TRACE AbstractConfiguration stopped 1 Appenders.
2015-09-23 08:06:30,076 TRACE AbstractConfiguration stopped 0 Loggers.
2015-09-23 08:06:30,076 DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@43db0439 OK
2015-09-23 08:06:30,083 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478
2015-09-23 08:06:30,087 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=StatusLogger
2015-09-23 08:06:30,089 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=ContextSelector
2015-09-23 08:06:30,092 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=Loggers,name=
2015-09-23 08:06:30,093 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=Loggers,name=my.package
2015-09-23 08:06:30,093 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=Loggers,name=my.test.package
2015-09-23 08:06:30,095 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=Appenders,name=Console
2015-09-23 08:06:30,096 DEBUG Registering MBean org.apache.logging.log4j2:type=672866478,component=Appenders,name=ApplicationLog
2015-09-23 08:06:30,096 DEBUG Reconfiguration complete for context[name=672866478] at null (org.apache.logging.log4j.core.LoggerContext@655b289) with optional ClassLoader: null
2015-09-23 08:06:30,097 DEBUG Shutdown hook enabled. Registering a new one.
2015-09-23 08:06:30,100 DEBUG LoggerContext[name=672866478, org.apache.logging.log4j.core.LoggerContext@655b289] started OK.

看起来和你面对的issue一样。

检查日志文件夹的文件夹权限并授予其写入权限。

chmod 666 logs

我通过源代码调试解决了这个问题,因为我的日志本应滚动更新。

显然,有一个未记录的属性可以放在名为 "append" 的 RollingFile 元素中,当设置为 false 时,它将写入当前日志文件从一开始就。遗憾的是,此属性的默认值为 true,这会导致当前日志文件被附加而不是重新启动。

所以我的最终 log4j2.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %c{36} - %msg%n"/>
        </Console>
        <RollingFile
            name="ApplicationLog"
            fileName="${sys:catalina.base}/logs/application.log"
            filePattern="${sys:catalina.base}/logs/application %d{yyyy-MM-dd}.log"
            append="false">
              <PatternLayout>
                  <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %c{36} %l: %msg%n</Pattern>
              </PatternLayout>
              <Policies>
                  <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
              </Policies>
              <DefaultRolloverStrategy/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="my.package" level="info" additivity="false">
            <AppenderRef ref="ApplicationLog"/>
        </Logger>
        <Logger name="my.test.package" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

希望这能帮助其他人在不到 3 天的时间内解决这个问题。

EDIT:结果证明这个属性被记录在here(table中的第一个参数),但它仍然没有对我来说为什么附加是默认行为是有意义的。为什么我希望我的文件大小永远增加!?反正不是重点,就是吐槽!

谢谢, 亚当