Slf4j LogBack
程序员文章站
2022-03-05 08:09:53
...
LogBack
概述
- Logback-core:1.1.2
配置示例
<logger name="LogController" level="INFO">
<appender-ref ref="LOG" />
</logger>
<appender name="LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_root}/alligatorAct.log</file>
<append>false</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志输出位置 可相对、和绝对路径 -->
<fileNamePattern>${log_root}/alligatorAct.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件假设设置每个月滚动,且<maxHistory>是6,
则只保存最近6个月的文件,删除之前的旧文件。注意,删除旧文件是,那些为了归档而创建的目录也会被删除-->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{'MM-dd HH:mm:ss,SSS',GMT+8:00}] %-5p [%.10t][%X{CU}] %logger{36}[%L] - %m%n</pattern>
</encoder>
</appender>
标签含义
- RollingFileAppender1
- file: 当天日志文件名称,如果没有配置,默认取回滚策略配置的文件名称
- append:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true
- TimeBasedRollingPolicy1
- fileNamePattern 配置归档日志的命名格式
- 按天 %d{yyyy-MM-dd}
- 按月 %d{yyyy-MM}
- maxHistory 如果回滚策略是月,此时配置的时间是月;如果回滚策略配置的是天,此处是天数
- cleanHistoryOnStart 是否删除历史归档日志,默认为 false
- fileNamePattern 配置归档日志的命名格式
- encoder和pattern节点组合用于具体输出的日志格式2
疑问梳理
- 为何配置了 maxHistory 历史归档日志没有被删除?
- 因为没有开启清除开关 cleanHistoryOnStart 配置 true
- TimeBasedRollingPolicy
public void start() {
this.renameUtil.setContext(this.context);
if (this.fileNamePatternStr != null) {
this.fileNamePattern = new FileNamePattern(this.fileNamePatternStr, this.context);
this.determineCompressionMode();
this.compressor = new Compressor(this.compressionMode);
this.compressor.setContext(this.context);
this.fileNamePatternWCS = new FileNamePattern(Compressor.computeFileNameStr_WCS(this.fileNamePatternStr, this.compressionMode), this.context);
this.addInfo("Will use the pattern " + this.fileNamePatternWCS + " for the active file");
if (this.compressionMode == CompressionMode.ZIP) {
String zipEntryFileNamePatternStr = this.transformFileNamePattern2ZipEntry(this.fileNamePatternStr);
this.zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, this.context);
}
if (this.timeBasedFileNamingAndTriggeringPolicy == null) {
this.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy();
}
this.timeBasedFileNamingAndTriggeringPolicy.setContext(this.context);
this.timeBasedFileNamingAndTriggeringPolicy.setTimeBasedRollingPolicy(this);
this.timeBasedFileNamingAndTriggeringPolicy.start();
if (this.maxHistory != 0) {
this.archiveRemover = this.timeBasedFileNamingAndTriggeringPolicy.getArchiveRemover();
this.archiveRemover.setMaxHistory(this.maxHistory);
if (this.cleanHistoryOnStart) { // 为 true 时才会清理历史归档日志
this.addInfo("Cleaning on start up");
// 清理历史归档日志
this.archiveRemover.clean(new Date(this.timeBasedFileNamingAndTriggeringPolicy.getCurrentTime()));
}
}
super.start();
} else {
this.addWarn("The FileNamePattern option must be set before using TimeBasedRollingPolicy. ");
this.addWarn("See also http://logback.qos.ch/codes.html#tbr_fnp_not_set");
throw new IllegalStateException("The FileNamePattern option must be set before using TimeBasedRollingPolicy. See also http://logback.qos.ch/codes.html#tbr_fnp_not_set");
}
}
- 同时配置rollingPolicy 与 triggeringPolicy 日志无法写入
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1MB</MaxFileSize>
</triggeringPolicy>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>/log/alligator/alligatorError.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
</rollingPolicy>
- 需要更改配置3
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/info.%d{yyyyMMdd}.%i.log</FileNamePattern>
<maxFileSize>100MB</maxFileSize>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
上一篇: SpringBoot日志文件配置
下一篇: Swap Nodes in Pairs
推荐阅读
-
Spring Boot异步输出Logback日志方法详解
-
Java日志Log4j或者Logback的NDC和MDC功能
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
利用SpringBoot+Logback手写一个简单的链路追踪
-
IntelliJ 注解@Slf4j后找不到log问题解决
-
动态替换logback FileAppender/RollingFileAppender 配置- Programmatically configure logback FileAppender/RollingBackAppender
-
springboot如何使用logback-spring配置日志格式,并分环境配置
-
JAVAEE——SpringBoot日志篇:日志框架SLF4j、日志配置、日志使用、切换日志框架
-
Logback配置文件这么写(TPS提高10倍)
-
Java日志框架之logback使用详解