日志框架
- 场景
- 用户下线
- 接口超时
- 数据库崩溃
- 输出目标
- 文件
- 定制输出格式
- 日志所携带的上下文信息
- 时间戳
- 线程
- 运行时选择性输出
- 灵活的配置
- 性能
常见日志框架
框架名称 | 日志门面 | 日志实现 |
---|---|---|
jdk自带jul | ✅ | |
Apache自带jcl | ✅ | |
Log4j | ✅ | |
Log4j2 | ✅ | |
Logback | ✅ | |
SLF4j | ✅ | |
jboss-logging | ✅ |
选择Logback(代码)和slf4j(门面)
简单使用
普通版本
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
代码 ```java package com.iming.sell;
import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @SpringBootTest public class LoggerTest {
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
@Test
public void test1() {
logger.info("info.....");
logger.debug("debug.....");
logger.error("error.....");
}
}
<a name="cqOg9"></a>
#### 简化版本
1. 新增依赖
```xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
@RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class LoggerTest {
// private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
@Test
public void test1() {
log.info("info.....");
log.debug("debug.....");
log.error("error.....");
}
}
<a name="PRuGz"></a>
### Logback配置(二选一即可)
<a name="8GLfe"></a>
#### application.yml配置(用于简单配置)
```yaml
// 简单配置 功能不多
logging:
pattern:
console: "%d - %msg%n" # 配置输出的格式
# path: /Users/chaowang/log/ # 配置日志的路径
file: /Users/chaowang/log/tomcat/sell.log # 配置日志的路径+名称
# level: debug # 配置日志的级别
level:
com.iming.sell.LoggerTest: debug # 类级别的debug
logback-spring.xml配置(用于复杂配置)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 单个配置项, class表示引用的类 -->
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<!-- 配置日志格式 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %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 精确匹配 -->
<!-- ERROR及以上被抛弃不再经过其他过滤器 -->
<!-- ERROR以下立即处理 不经过其他过滤器 -->
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>/Users/chaowang/log/tomcat/sell/info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<!-- 配置日志路径 -->
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 配置过滤 -->
<!-- ThresholdFilter 临界值过滤器,过滤掉低于指定临界值的日志。当日志级别等于或高于临界值时,过滤器返回NEUTRAL;当日志级别低于临界值时,日志会被拒绝。 -->
<!-- 等价与level = info,onMatch = NEUTRAL, onDismatch = deny -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- level大于Error 都会显示 -->
<Level>
ERROR
</Level>
</filter>
<!-- -->
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>/Users/chaowang/log/tomcat/sell/error.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<!-- 使用配置 -->
<root level="info">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
</root>
</configuration>
资料:https://juejin.im/post/5b51f85c5188251af91a7525#heading-8