本文最后更新于:2021年9月29日 凌晨
概览:Slf4j日志
Slf4j + logback
2021/08/08
基本知识
等级从低到高分别是TRACE < DEBUG < INFO < WARN < ERROR。
日志输出的时候,级别大的会输出,根据当前ROOT 级别,日志输出时,级别高于root默认的级别时会输出,比如如果root的级别是info,那么会输出info以及info级别以上的日志。
也就是说,如果root那里的level配置的info级别的,那么warn对应的日志也可以输出。
http://www.51gjie.com/javaweb/1109.html
Maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.3</version> <scope>runtime</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> <scope>runtime</scope> </dependency>
|
日志配置文件
logback.xml文件放置在resources目录下即可。
logback.xml简单版内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender>
<root level="info"> <appender-ref ref="STDOUT"/> </root> <logger name="com.learn.demo5.dao" level="DEBUG"/> </configuration>
|
或者:
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss} %5p %c:%L] %m%n</pattern> </encoder> </appender>
<root level="info"> <appender-ref ref="console" /> </root> </configuration>
|
复杂版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| <?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false"> <property name="log.level" value="debug" /> <property name="log.maxHistory" value="60" /> <property name="log.filePath" value="/opt/logs" /> <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n"></property>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> </appender>
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.filePath}/debug.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern> ${log.filePath}/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>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.filePath}/info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern> ${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz </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>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.filePath}/error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern> ${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz </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>
<logger name="com.campus.o2o" level="${log.level}" additivity="true"> <appender-ref ref="debugAppender" /> <appender-ref ref="infoAppender" /> <appender-ref ref="errorAppender" /> </logger>
<root lever="info"> <appender-ref ref="consoleAppender" /> </root>
</configuration>
|
lombok的日志
lombok依赖
1 2 3 4 5
| <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
|
log配置文件:logback.xml
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss} %5p %c:%L] %m%n</pattern> </encoder> </appender>
<root level="info"> <appender-ref ref="console" /> </root> </configuration>
|
日志使用:直接使用@Slf4j
注解,然后再直接使用log对象即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.example.springbootdem0.Service;
import com.example.springbootdem0.entity.Stu; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;
@Service @Slf4j public class TestStu {
public int func(){ log.info("func run ..."); Stu stu = Stu.builder() .id(1) .age(18) .name("小明") .mark("备注").build(); log.info("生成一个stu对象:{}",stu.toString()); return 0; } }
|