logback-spring.xml 模版
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<springProperty scope="context" name="LOG_HOME" source="log.path"/>
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<!-- 日志输出格式 -->
<property name="PATTERN" value=" %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] [%X{requestId}] %logger{50} [%line] - %msg%n"/>
<!-- 控制台日志:输出全部日志到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 文件日志:输出全部日志到文件 -->
<appender name="FILE_INFO_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/info-${APP_NAME}.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/info-${APP_NAME}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>180</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 错误日志:用于将错误日志输出到独立文件 -->
<appender name="FILE_ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/error-${APP_NAME}.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/error-${APP_NAME}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>180</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--必填标签,用来指定最基础的日志输出级别-->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE_INFO_LOG" />
<appender-ref ref="FILE_ERROR_LOG" />
</root>
</configuration>
部分标签说明
-
<root>
标签,必填标签,用来指定最基础的日志输出级别 -
<appender-ref>
标签,添加append -
<append>
标签,通过使用该标签指定日志的收集策略name
属性指定appender命名class
属性指定输出策略,通常有两种,控制台输出和文件输出,文件输出就是将日志进行一个持久化。ConsoleAppender将日志输出到控制台 -
<filter>
标签,通过使用该标签指定过滤策略 -
<level>
标签指定过滤的类型 -
<encoder>
标签,使用该标签下的标签指定日志输出格式 -
<rollingPolicy>
标签指定收集策略,比如基于时间进行收集 -
<fileNamePattern>
标签指定生成日志保存地址,通过这样配置已经实现了分类分天手机日志的目标了
application.properties
中配置
log.path = ./logs/spring-cloud-project-b