- Pom引用依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>++++logback++++ -classic</artifactId>
<version>1.2.11</version>
</dependency>
- logback.xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<!-- stdout start 所有信息输出到控制台 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
</encoder>
</appender>
<!-- stdout end -->
<appender name="debug-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
<fileNamePattern>logs/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
<MaxFileSize>100KB</MaxFileSize>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
</appender>
<appender name="info-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
<fileNamePattern>logs/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
<MaxFileSize>100KB</MaxFileSize>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
</appender>
<appender name="error-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
<fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1MB</totalSizeCap>
<MaxFileSize>100KB</MaxFileSize>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
</appender>
<!-- <appender name="log-trace-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<File>${logPath}/log_trace.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logPath}/log_trace.log.%d{yyyy-MM-dd-HH}</FileNamePattern>
<maxHistory>24</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender> -->
<!-- 针对个别包日志可以进行单独的设置级别,用于过滤 -->
<!-- <logger name="log-trace" additivity="false" level="INFO">
<appender-ref ref="log-trace-rolling"/>
</logger>
<logger name="org.apache.zookeeper.ZooKeeper" additivity="false" level="ERROR">
<appender-ref ref="time-rolling"/>
</logger>
<logger name="org.apache.zookeeper.ClientCnxn" additivity="false" level="ERROR">
<appender-ref ref="time-rolling"/>
</logger> -->
<root level="DEBUG">
<appender-ref ref="debug-rolling"/>
<appender-ref ref="info-rolling"/>
<appender-ref ref="error-rolling"/>
<appender-ref ref="stdout"/>
</root>
</configuration>
详细可到以下链接下载