现在主流的都使用logback+slf4j进行日志打印。
slf4j 需要配置maven
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
logback-spring.xml
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d{HH:mm:ss.SSS} %-5level %logger{80} - %msg%n </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!-- 滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件路径--> <fileNamePattern> /mfw_data/imooc_log/study/springboot/info.%d.log </fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 根据范围过滤--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!-- 滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件路径--> <fileNamePattern> /mfw_data/imooc_log/study/springboot/error.%d.log </fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"></appender-ref> <appender-ref ref="fileInfoLog"></appender-ref> <appender-ref ref="fileErrorLog"></appender-ref> </root> </configuration>
一个简单的logback-spring.xml配置文件
输出日志地址: