日志框架

  1. 场景
  • 用户下线
  • 接口超时
  • 数据库崩溃
  1. 输出目标
  • 文件
  1. 定制输出格式
  2. 日志所携带的上下文信息
  • 时间戳
  • 线程
  1. 运行时选择性输出
  2. 灵活的配置
  3. 性能

常见日志框架

框架名称 日志门面 日志实现
jdk自带jul
Apache自带jcl
Log4j
Log4j2
Logback
SLF4j
jboss-logging

选择Logback(代码)和slf4j(门面)

简单使用

普通版本

  1. 依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-test</artifactId>
    4. <scope>test</scope>
    5. </dependency>
    6. <dependency>
    7. <groupId>junit</groupId>
    8. <artifactId>junit</artifactId>
    9. <version>4.12</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-test</artifactId>
    14. </dependency>
    15. <dependency>
    16. <groupId>org.springframework</groupId>
    17. <artifactId>spring-test</artifactId>
    18. <version>5.1.8.RELEASE</version>
    19. <scope>compile</scope>
    20. </dependency>
  2. 代码 ```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 {

  1. private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
  2. @Test
  3. public void test1() {
  4. logger.info("info.....");
  5. logger.debug("debug.....");
  6. logger.error("error.....");
  7. }

}

  1. <a name="cqOg9"></a>
  2. #### 简化版本
  3. 1. 新增依赖
  4. ```xml
  5. <dependency>
  6. <groupId>org.projectlombok</groupId>
  7. <artifactId>lombok</artifactId>
  8. <optional>true</optional>
  9. </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);

  1. @Test
  2. public void test1() {
  3. log.info("info.....");
  4. log.debug("debug.....");
  5. log.error("error.....");
  6. }

}

  1. <a name="PRuGz"></a>
  2. ### Logback配置(二选一即可)
  3. <a name="8GLfe"></a>
  4. #### application.yml配置(用于简单配置)
  5. ```yaml
  6. // 简单配置 功能不多
  7. logging:
  8. pattern:
  9. console: "%d - %msg%n" # 配置输出的格式
  10. # path: /Users/chaowang/log/ # 配置日志的路径
  11. file: /Users/chaowang/log/tomcat/sell.log # 配置日志的路径+名称
  12. # level: debug # 配置日志的级别
  13. level:
  14. com.iming.sell.LoggerTest: debug # 类级别的debug

logback-spring.xml配置(用于复杂配置)

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <!-- 单个配置项, class表示引用的类 -->
  4. <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
  5. <!-- 配置日志格式 -->
  6. <layout class="ch.qos.logback.classic.PatternLayout">
  7. <pattern>
  8. %d - %msg%n
  9. </pattern>
  10. </layout>
  11. </appender>
  12. <!-- 配置日志路径 -->
  13. <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
  14. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  15. <Level>
  16. ERROR
  17. </Level>
  18. <!-- onMatch 精确匹配 -->
  19. <!-- ERROR及以上被抛弃不再经过其他过滤器 -->
  20. <!-- ERROR以下立即处理 不经过其他过滤器 -->
  21. <onMatch>DENY</onMatch>
  22. <onMismatch>ACCEPT</onMismatch>
  23. </filter>
  24. <encoder>
  25. <pattern>
  26. %msg%n
  27. </pattern>
  28. </encoder>
  29. <!-- 滚动策略 -->
  30. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  31. <!-- 路径 -->
  32. <fileNamePattern>/Users/chaowang/log/tomcat/sell/info.%d.log</fileNamePattern>
  33. </rollingPolicy>
  34. </appender>
  35. <!-- 配置日志路径 -->
  36. <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
  37. <!-- 配置过滤 -->
  38. <!-- ThresholdFilter 临界值过滤器,过滤掉低于指定临界值的日志。当日志级别等于或高于临界值时,过滤器返回NEUTRAL;当日志级别低于临界值时,日志会被拒绝。 -->
  39. <!-- 等价与level = info,onMatch = NEUTRAL, onDismatch = deny -->
  40. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  41. <!-- level大于Error 都会显示 -->
  42. <Level>
  43. ERROR
  44. </Level>
  45. </filter>
  46. <!-- -->
  47. <encoder>
  48. <pattern>
  49. %msg%n
  50. </pattern>
  51. </encoder>
  52. <!-- 滚动策略 -->
  53. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  54. <!-- 路径 -->
  55. <fileNamePattern>/Users/chaowang/log/tomcat/sell/error.%d.log</fileNamePattern>
  56. </rollingPolicy>
  57. </appender>
  58. <!-- 使用配置 -->
  59. <root level="info">
  60. <appender-ref ref="consoleLog" />
  61. <appender-ref ref="fileInfoLog" />
  62. <appender-ref ref="fileErrorLog" />
  63. </root>
  64. </configuration>

资料:https://juejin.im/post/5b51f85c5188251af91a7525#heading-8