logback 的使用说明
1、maven 依赖配置
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <logback.version>1.1.7</logback.version> <slf4j.version>1.7.21</slf4j.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies>
2、logback 配置
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoding>UTF-8</encoding> <encoder> <pattern> [%d{yyyy-MM-dd HH:mm:ss}] %5p %c:%L - %m%n </pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> </appender> <appender name="logback-demo-info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${log.dirs}/logback-demo-info.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.dirs}/net5decoration-info.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>600 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </pattern> </encoder> </appender> <appender name="logback-demo-error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件名 --> <File>${log.dirs}/logback-demo-error.log</File> <!-- 设置文件切割策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件备份的名字 --> <fileNamePattern>${log.dirs}/net5decoration-error.log.%d{yyyy-MM-dd}.gz</fileNamePattern> <!-- 超过 600M 就压缩备份--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>600 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- 保留最多 30 天的历史日志记录--> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <--mybatis 的日志输出级别配置--> <logger name="com.*.mapper" level="debug"/> <!-- error 级别日志的配置 --> <logger name="errorAppender" additivity="false" level="ERROR"> <appender-ref ref="logback-demo-error"/> </logger> <root level="INFO"> <appender-ref ref="logback-demo-info"/> <appender-ref ref="console"/> </root> </configuration>
3、在 java 文件中的使用
private static final Logger logger = LoggerFactory.getLogger(ClassName.class);
logger.error("");
logger.info("");
具体的更详细的介绍可以看看官网
https://logback.qos.ch/manual/index.html
或者看看这哥们写的,也很详细