logback.xml 判断
时间: 2023-12-16 07:28:13 浏览: 78
logback.xml是一个用于配置logback日志框架的XML文件。在logback.xml中,可以配置输出源appender、打日志的logger(子节点)和root(根节点),以及日志级别Level等信息。具体来说,logback.xml中的子节点如果有输出源直接输入,如果无,则判断配置的additivity,是否向上级传递,即是否向root传递,传递则采用root的输出源,否则不输出日志。另外,additivity属性用于控制子Logger是否继承父Logger的appender,默认情况下子Logger会继承父Logger的appender,若是additivity设为false,则子Logger只会在自己的appender里输出,而不会在父Logger的appender里输出。在logback.xml中还可以使用<property>子节点来定义属性,这些属性可以在logback.xml中被引用。
相关问题
logback指定时间执行压缩
要使用logback实现定时压缩日志文件,可以使用logback的`TimeBasedRollingPolicy`和`SizeAndTimeBasedFNATP`。具体步骤如下:
1. 在logback.xml中配置`TimeBasedRollingPolicy`和`SizeAndTimeBasedFNATP`,如下所示:
```xml
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/mylog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
```
2. 在logback.xml中配置定时任务,使用`ch.qos.logback.core.util.FileSize`来判断文件大小,如下所示:
```xml
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- appender配置... -->
</appender>
<scheduledThreadPool>
<scheduledExecutorService class="java.util.concurrent.Executors"
name="scheduled-thread-pool"/>
</scheduledThreadPool>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
<timeBasedRollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>30</maxHistory>
</timeBasedRollingPolicy>
<scheduledFilename>logs/mylog-%d{yyyy-MM-dd}.log.gz</scheduledFilename>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<timeBasedArchiveRemover class="ch.qos.logback.core.rolling.helper.DefaultArchiveRemover">
<maxHistory>30</maxHistory>
</timeBasedArchiveRemover>
<scheduledFuture>
<scheduledFuture>
<cronExpression>0 0 0 * * ? *</cronExpression>
<fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<scheduledExecutorService class="java.util.concurrent.Executors"
name="scheduled-thread-pool"/>
<fileSize>${FileSize.valueOf("1MB")}</fileSize>
</scheduledFuture>
</scheduledFuture>
</timeBasedFileNamingAndTriggeringPolicy>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
```
上述配置中,`<cronExpression>0 0 0 * * ? *</cronExpression>`指定了每天0点执行一次定时任务,`${FileSize.valueOf("1MB")}`指定了文件大小,当日志文件大小达到1MB时,就会触发日志文件的压缩。
阅读全文