<configuration scan="false" scanPeriod="10 seconds">
<!-- 定义日志存放的根目录 -->
<property name="log.dir" value="./logs" />
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="console.encoder"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss}) %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%5.5t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 属性声明:普通日志打印写log文件用-->
<property name = "file.encoder" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"></property>
<!-- 控制台打印输出规则包含info error warn-->
<appender name = "console" class = "ch.qos.logback.core.ConsoleAppender" level="info">
<encoder>
<pattern>${console.encoder}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 滚动策略 debug info error warn -->
<appender name="debug-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件名 目录结构为logs文件夹下 名字叫debug.log的日志文件-->
<file>${log.dir}/debug.log</file>
<encoder>
<pattern>${file.encoder}</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 滚动策略如果logs文件夹下的debug.log超过maxFileSize 就会保存到fileNamePattern目录 命名就在fileNamePattern内也就是 0,2,3这样-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
<fileNamePattern>${log.dir}/debug/%d{yyyy-MM-dd}/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10kb</maxFileSize>
<!-- 最大保存体积超过自动删除老的日志文件让其始终保持最多5gb-->
<totalSizeCap>5GB</totalSizeCap>
<!-- 无论超不超5gb但凡超过60天就删-->
<maxHistory>60</maxHistory>
</rollingPolicy>
<!-- 过滤只要debug类型的日志-->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 级别-->
<level>debug</level>
<!-- 只要debug-->
<onMatch>ACCEPT</onMatch>
<!-- 其他的都不要-->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 与debug appender 一样不做过多解释-->
<appender name="info-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/info.log</file>
<encoder>
<pattern>${file.encoder}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
<fileNamePattern>${log.dir}/info/%d{yyyy-MM-dd}/info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10kb</maxFileSize>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>60</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 与debug appender 一样不做过多解释-->
<appender name="warn-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/warn.log</file>
<encoder>
<pattern>${file.encoder}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
<fileNamePattern>${log.dir}/warn/%d{yyyy-MM-dd}/warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>60</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 与debug appender 一样不做过多解释-->
<appender name="error-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/error.log</file>
<encoder>
<pattern>${file.encoder}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
<fileNamePattern>${log.dir}/error/%d{yyyy-MM-dd}/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>60</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- additivity标识不给root类 只给自定义的附加器-->
<logger name="com.self.mapper" level="debug" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="debug-file" />
</logger>
<!-- 打印事务、mapper注册等信息,打印到控制台、记录到debug_file additivity标识不给root类 只给自定义的附加器-->
<logger name="org.mybatis.spring.SqlSessionUtils" level="DEBUG" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="debug-file"/>
</logger>
<!-- root标识log父类 包含了所有类型日志这里的作用是把日志给哪些附加器授权用-->
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="debug-file"/>
<appender-ref ref="info-file"/>
<appender-ref ref="warn-file"/>
<appender-ref ref="error-file"/>
</root>
</configuration>
生成日志如下:
目录 :按照日志类型-日期
文件命名:-类型+日期+索引+.log