福田专业网站建设公司哪家好,做油和米的网站,怎么查网站备案域名备案信息,网站建设百度认证图片Log4j2 介绍
Spring Boot 中默认使用 Logback 作为日志框架#xff0c;接下来我们将学习如何在 Spring Boot 中集成与配置 Log4j2。在配置之前#xff0c;我们需要知道的是 Log4j2 是 Log4j 的升级版#xff0c;它在 Log4j 的基础上做了诸多改进#xff1a;
异步日志接下来我们将学习如何在 Spring Boot 中集成与配置 Log4j2。在配置之前我们需要知道的是 Log4j2 是 Log4j 的升级版它在 Log4j 的基础上做了诸多改进
异步日志支持 Java8 lambda 风格的懒加载日志过滤器插件并发性改进支持 SLF4J, Commons Logging, Log4j-1.x 以及 java.util.logging;配置热加载;自定义日志级别
看到上面这些新特性我们肯定特别想在我们的 Spring Boot 应用中使用 Log4j2
添加 Maven 依赖
Spring Boot 默认使用的是 logback, 想要使用 Log4j2, 我们需要首先排除掉默认的日志框架然后添加 log4j2 依赖下面是 pom.xml 文件
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdexclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-logging/artifactId/exclusion/exclusions
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-log4j2/artifactId
/dependency
添加 Log4j2 配置文件
Spring Boot 支持以下 4 种格式的配置文件
xml默认的jsonyamlproperties 文件
Spring Boot 如果在 classpath:目录下找到了 log4j2.xml 或者 log4j2.json 或者 log4j2.properties或者log4j2.yaml的其中任意一个配置文件就会自动加载并使用它。 接下来我们来看看 log4j2.xml 格式要如何配置 在 /src/main/resource 目录下创建 log4j2.xml 配置文件
?xml version1.0 encodingUTF-8?
Configuration statusWARN monitorInterval30PropertiesProperty namePID????/PropertyProperty nameLOG_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/PropertiesAppendersConsole nameConsole targetSYSTEM_OUT followtruePatternLayout pattern${LOG_PATTERN}//Console/AppendersLoggersRoot levelinfoAppenderRef refConsole//Root/Loggers
/Configuration