Spring Boot 配置 log4j2

[复制链接]
发表于 2023-10-16 07:50:16 | 显示全部楼层 |阅读模式

本教程中,我们将学习如何在 Spring Boot 中整合使用 Log4j2 日志日志框架。
Log4j2 介绍

Spring Boot 中默认使用 Logback 作为日志日志框架,接下来我们将学习如何在 Spring Boot 中集成与配置 Log4j2。在配置之前,我们需要知道的是 Log4j2 是 Log4j 的升级版,它在 Log4j 的基础上做了诸多改进:

  • 1.异步日志日志
  • 2.支持 Java8 lambda 风格的懒加载日志;
  • 3.过滤器;
  • 4.插件;
  • 5.并发性改进;
  • 6.支持: SLF4J, Commons Logging, Log4j-1.x 以及 java.util.logging;
  • 7.配置热加载;
  • 8.自定义日志级别;
看到上面这些新特性,我们肯定特别想在我们的 Spring Boot 应用中使用 Log4j2
添加 Maven 依赖

Spring Boot 默认使用的是 logback, 想要使用 Log4j2, 我们需要首先排除掉默认的日志框架,然后添加 log4j2 依赖,下面是 pom.xml 文件:
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter</artifactId>
  4.     <exclusions>
  5.         <exclusion>
  6.             <groupId>org.springframework.boot</groupId>
  7.             <artifactId>spring-boot-starter-logging</artifactId>
  8.         </exclusion>
  9.     </exclusions>
  10. </dependency>
  11. <dependency>
  12.     <groupId>org.springframework.boot</groupId>
  13.     <artifactId>spring-boot-starter-log4j2</artifactId>
  14. </dependency>
复制代码
添加 Gradle 依赖(可选)

如果你是通过 Gradle 来构建项目的,看下面:
  1. dependencies {
  2.     compile 'org.springframework.boot:spring-boot-starter-log4j2'
  3. }
  4. configurations {
  5.     all {
  6.         exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
  7.     }
  8. }
复制代码
添加 Log4j2 配置文件

Spring Boot 支持以下 4 种格式的配置文件:

  • 1.xml(默认的)
  • 2.json
  • 3.yaml
  • 4.properties 文件
Spring Boot 如果在 classpath:目录下找到了 log4j2.xml 或者 log4j2.json 或者 log4j2.properties或者log4j2.yaml的其中任意一个配置文件,就会自动加载并使用它。
接下来,我们来看看 log4j2.xml 格式,要如何配置?
在 /src/main/resource 目录下创建 log4j2.xml 配置文件:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN" monitorInterval="30">
  3.     <Properties>
  4.         <Property name="PID">????</Property>
  5.         <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
  6.     </Properties>
  7.     <Appenders>
  8.         <Console name="Console" target="SYSTEM_OUT" follow="true">
  9.             <PatternLayout pattern="${LOG_PATTERN}"/>
  10.         </Console>
  11.     </Appenders>
  12.     <Loggers>
  13.         <Root level="info">
  14.             <AppenderRef ref="Console"/>
  15.         </Root>
  16.     </Loggers>
  17. </Configuration>
复制代码
通过 Properties 文件来创建配置文件(可选)

如果你不喜欢通过 xml 的方式,你还可以选择在 /src/main/resources 目录下创建 log4j2.properties:
  1. status = error
  2. name = Log4j2Sample
  3. appenders = console
  4. appender.console.type = Console
  5. appender.console.name = STDOUT
  6. appender.console.layout.type = PatternLayout
  7. appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} - %msg%n
  8. rootLogger.level = warn
  9. rootLogger.appenderRefs = stdout
  10. rootLogger.appenderRef.stdout.ref = STDOUT
复制代码
Log4j2 实战

一切准备就绪后,我们在 Application 启动类中,创建一个测试的接口,在接口中打印日志:
  1. package site.exception.springbootlog4j2;
  2. import org.apache.logging.log4j.LogManager;
  3. import org.apache.logging.log4j.Logger;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @SpringBootApplication
  9. @RestController
  10. public class SpringBootLog4j2Application {
  11.     public static void main(String[] args) {
  12.         SpringApplication.run(SpringBootLog4j2Application.class, args);
  13.     }
  14.     private static final Logger LOG = LogManager.getLogger(SpringBootLog4j2Application.class);
  15.     @GetMapping("/test")
  16.     public String test() {
  17.         LOG.debug("debug 级别日志 ...");
  18.         LOG.info("info 级别日志 ...");
  19.         LOG.warn("warn 级别日志 ...");
  20.         LOG.error("error 级别日志 ...");
  21.         LOG.fatal("fatal 级别日志 ...");
  22.         return "Hello Log4j2 !";
  23.     }
  24. }
复制代码
启动项目,看看控制台输出:
  1. 2023-10-06 22:15:59.815 INFO 11936 --- [nio-8080-exec-1] s.e.s.SpringBootLog4j2Application : info 级别日志 ...
  2. 2023-10-06 22:15:59.815 WARN 11936 --- [nio-8080-exec-1] s.e.s.SpringBootLog4j2Application : warn 级别日志 ...
  3. 2023-10-06 22:15:59.815 ERROR 11936 --- [nio-8080-exec-1] s.e.s.SpringBootLog4j2Application : error 级别日志 ...
  4. 2023-10-06 22:15:59.815 FATAL 11936 --- [nio-8080-exec-1] s.e.s.SpringBootLog4j2Application : fatal 级别日志 ...
复制代码
最后

欢迎大家关注我【小白技术圈】,回复 B01或b01,双手奉上Java相关书籍和面试题!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表