在下面的示例中,每次重新启动服务器时,它都会创建一个在归档目录下压缩的日志。
如何指定日志文件每天只存档一次,并且日志文件的大小优于maxFileSize中指定的值
<appender name="SERVICE-PROVIDER-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/log/test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- We are rolling over daily -->
<fileNamePattern>${LOG_HOME}/log/archived/test-%d{yyyy-MM-dd}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 10 days' worth of history capped at 1GB total size -->
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%thread] [%-20.20C:%-5.5L] %msg%n</Pattern>
</encoder>
</appender>
答案 0 :(得分:0)
我无法重现你的问题。我用你的配置创建了一个最小的运行时:
https://github.com/riskop/logback_timeandsizebased_archived_oldstyle.git
这是我的结果:
riskop@riskop:~/git$ git clone https://github.com/riskop/logback_timeandsizebased_archived_oldstyle.git
Cloning into 'logback_timeandsizebased_archived_oldstyle'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 20 (delta 2), reused 17 (delta 2), pack-reused 0
Unpacking objects: 100% (20/20), done.
Checking connectivity... done.
riskop@riskop:~/git$ cd logback_timeandsizebased_archived_oldstyle/
riskop@riskop:~/git/logback_timeandsizebased_archived_oldstyle$ mvn -q -DLOG_HOME=loghome test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running pack.TestStart
11:01:18,720 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:01:18,720 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
11:01:18,720 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/riskop/git/logback_timeandsizebased_archived_oldstyle/target/test-classes/logback.xml]
11:01:18,753 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
11:01:18,754 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
11:01:18,758 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [SERVICE-PROVIDER-LOG]
11:01:18,769 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - setting totalSizeCap to 1 GB
11:01:18,790 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - Will use gz compression
11:01:18,791 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - Will use the pattern loghome/log/archived/test-%d{yyyy-MM-dd}.%i.log for the active file
11:01:18,793 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - The date pattern is 'yyyy-MM-dd' from file name pattern 'loghome/log/archived/test-%d{yyyy-MM-dd}.%i.log.gz'.
11:01:18,793 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - Roll-over at midnight.
11:01:18,794 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - Setting initial period to Tue Mar 13 11:01:18 CET 2018
11:01:18,795 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
11:01:18,795 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
11:01:18,806 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[SERVICE-PROVIDER-LOG] - Active log file name: loghome/log/test.log
11:01:18,806 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[SERVICE-PROVIDER-LOG] - File property is set to [loghome/log/test.log]
11:01:18,806 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
11:01:18,807 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [SERVICE-PROVIDER-LOG] to Logger[ROOT]
11:01:18,807 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
11:01:18,807 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5e3a8624 - Registering current configuration as safe fallback point
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.544 sec - in pack.TestStart
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
riskop@riskop:~/git/logback_timeandsizebased_archived_oldstyle$ ls -l loghome/log/
total 1052
-rw-rw-r-- 1 riskop riskop 1074890 márc 13 11:01 test.log
riskop@riskop:~/git/logback_timeandsizebased_archived_oldstyle$ mvn -q -DLOG_HOME=loghome test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running pack.TestStart
11:01:27,654 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:01:27,655 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
11:01:27,655 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/riskop/git/logback_timeandsizebased_archived_oldstyle/target/test-classes/logback.xml]
11:01:27,698 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
11:01:27,699 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
11:01:27,705 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [SERVICE-PROVIDER-LOG]
11:01:27,722 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - setting totalSizeCap to 1 GB
11:01:27,745 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - Will use gz compression
11:01:27,747 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006485584 - Will use the pattern loghome/log/archived/test-%d{yyyy-MM-dd}.%i.log for the active file
11:01:27,749 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - The date pattern is 'yyyy-MM-dd' from file name pattern 'loghome/log/archived/test-%d{yyyy-MM-dd}.%i.log.gz'.
11:01:27,749 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - Roll-over at midnight.
11:01:27,751 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - Setting initial period to Tue Mar 13 11:01:20 CET 2018
11:01:27,752 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
11:01:27,752 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1bce4f0a - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
11:01:27,763 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[SERVICE-PROVIDER-LOG] - Active log file name: loghome/log/test.log
11:01:27,763 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[SERVICE-PROVIDER-LOG] - File property is set to [loghome/log/test.log]
11:01:27,764 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
11:01:27,764 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [SERVICE-PROVIDER-LOG] to Logger[ROOT]
11:01:27,765 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
11:01:27,765 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5e3a8624 - Registering current configuration as safe fallback point
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.568 sec - in pack.TestStart
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
riskop@riskop:~/git/logback_timeandsizebased_archived_oldstyle$ ls -l loghome/log/
total 2100
-rw-rw-r-- 1 riskop riskop 2149780 márc 13 11:01 test.log
日志条目附加到test.log,如第一次运行后的test.log长度约为1M。第二次运行测试test.log后为2M。
但请注意,不推荐使用SizeAndTimeBasedFNATP。
答案 1 :(得分:0)
您使用的是哪个版本的Logback?如果您低于版本1.1.7,则应考虑更新,因为这将使您能够使用SizeAndTimeBasedRollingPolicy。
我有一个类似的问题,我的解决方案是使用SizeAndTimeBasedRollingPolicy,如下所示:
<appender name="SERVICE-PROVIDER-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${LOG_HOME}/log/archived/test-%d{yyyy-MM-dd}.%i.log.gz
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%thread] [%-20.20C:%-5.5L] %msg%n</Pattern>
</encoder>
</appender>
希望这有帮助!