针对于Springboot项目,设置 logback日志信息格式化打印
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xml>
<configuration scan="true">
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="utf-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] [%file : %line] - %msg%n</pattern>
</encoder>
</appender>
<property name="infoPath" value="/opt/logs/info"></property>
<property name="errorPath" value="/opt/logs/error"></property>
<property name="maxFileSize" value="5MB"/>
<!-- INFO级别输出 -->
<appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${infoPath}/isomp-mgr-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天一归档 -->
<fileNamePattern>${infoPath}/isomp-mgr-info-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder charset="utf-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] [%file : %line] - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- ERROR级别输出 -->
<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${errorPath}/isomp-mgr-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天一归档 -->
<fileNamePattern>${errorPath}/isomp-mgr-error-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder charset="utf-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] [%file : %line] - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 优先级从高到低分别是ERROR、WARN、INFO、DEBUG -->
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="info"/>
<appender-ref ref="error"/>
</root>
</configuration>
2021-03-03 完善
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--控制按天归档日志文件-->
<FileNamePattern>${logDir}/error.${appName}.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--控制要保存的文件数量,超过MaxHistory数量才会删除,只有当每天生成且只生成一个文件时才表示保留天数-->
<maxHistory>180</maxHistory>
<!-- 控制所有存档文件的总大小 -->
<!-- <totalSizeCap>1GB</totalSizeCap> -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai} [%thread] %-5level %logger{36} %line - %msg%n</pattern>
</encoder>
以上代码想要强调的场景:
(1)按天归档日志且只保留180天的历史日志
maxHistory:指的是文件数量,超过MaxHistory数量才会删除,只有当每天生成且只生成一个文件时才表示保留天数。
cleanHistoryOnStart:默认false,若设置为true后保证项目启动时自动删除多余日志文件。
二者同时使用更完美哦!
(2)Logback的时区设置为UTC+8/GMT+8(Asia/Shanghai)
%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai}