spring-boot系列——使用log4j2

桃扇骨 2022-07-12 15:22 267阅读 0赞
  1. 原创文章,转载请注明
  2. spring-boot中使用log4j2其实非常简单,因为spring-boot本身也使用了,而且还不止log4j2。我们如果要用的话必须把spring-boot-starter-logging的依赖去掉,不然会是spring-boot默认的日志,该starterspring-boot-starter中。既然去除了官方的日志依赖,同时也去除了log4j2的依赖,因此我们还需要把log4j2的依赖加进来。
  3. 之后就可以自己配置log4j2了,官方的说明文档在76.2章节。
  4. 摘录如下:

76.2 Configure Log4j for logging

Spring Boot supports Log4j 2 for loggingconfiguration if it is on the classpath. If you are using the starters forassembling dependencies that means you have to exclude Logback and then include log4j 2instead. If you aren’t using the starters then you need to provide jcl-over-slf4j(at least) in addition to Log4j 2.

The simplest path is probably through the starters, even though it requires somejiggling with excludes, .e.g. in Maven:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter</artifactId>
  8. <exclusions>
  9. <exclusion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-logging</artifactId>
  12. </exclusion>
  13. </exclusions>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-log4j2</artifactId>
  18. </dependency>









[Note]

The use of the Log4j starters gathers together the dependencies for common loggingrequirements (e.g. including having Tomcat use java.util.logging but configuring theoutput using Log4j 2). See the Actuator Log4j 2 samples for more detail and to see it inaction.

#

76.2.1 Use YAML or JSON to configure Log4j 2

In addition to its default XML configuration format, Log4j 2 also supports YAML and JSONconfiguration files. To configure Log4j 2 to use an alternative configuration file format,add the appropriate dependencies to the classpath and name yourconfiguration files to match your chosen file format:





















Format Dependencies File names

YAML

com.fasterxml.jackson.core:jackson-databind com.fasterxml.jackson.dataformat:jackson-dataformat-yaml

log4j2.yaml log4j2.yml

JSON

com.fasterxml.jackson.core:jackson-databind

log4j2.json log4j2.jsn

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot整合log4j2

    相比与其他的日志系统,log4j2丢数据这种情况少;disruptor技术,在多线程环境下,性能高于logback等10倍以上;利用jdk1.5并发的特性,减少了死锁的发生;

    相关 springboot集成Log4j2

    前言:关于Log4j2,SLF4J,LogBack以及其他的一些日志框架的区别,建议大家可以先了解一下 一、添加依赖 <!--SpringBoot默认日志框架为