logback配置日志滚动分类存储
导入logback日志依赖后会自动导入slf4j的api包
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
logback.xml配置
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志级别TRACE、DEBUG、INFO、WARN、ERROR -->
<property name="log.level" value="TRACE"/>
<!-- 最多保留近30天日志 -->
<property name="log.maxHistory" value="30"/>
<!-- 日志的存放位置 可用${catalina.base}表示tomcat根目录 -->
<property name="log.root" value="C:/Users/Xiaoming/Desktop/logs/"/>
<!-- 日志格式 -->
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
<!-- 控制台输出 -->
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- DEBUG -->
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件路径 -->
<file>${log.root}/debug.log</file><!-- 当前(当天)日志文件存放路径 -->
<!-- 日志滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 历史日志文件的存放路径和名称 -->
<fileNamePattern>${log.root}/debug/debug-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志文件最大的保存历史 数量-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- INFO -->
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件路径 -->
<file>${log.root}/info.log</file><!-- 当前(当天)日志文件存放路径 -->
<!-- 日志滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 历史日志文件的存放路径和名称 -->
<fileNamePattern>${log.root}/info/info-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 历史日志保留数量-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- ERROR-->
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件路径 -->
<file>${log.root}/error.log</file><!-- 当前(当天)日志文件存放路径 -->
<!-- 日志滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 历史日志文件的存放路径和名称 -->
<fileNamePattern>${log.root}/error/error-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 历史日志保留数量-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="${log.level}">
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debugAppender"/>
<appender-ref ref="infoAppender"/>
<appender-ref ref="errorAppender"/>
</root>
</configuration>