曹耘豪的博客

Spring之使用log4j2和slf4j

  1. 引入依赖
  2. 配置log4j

引入依赖

1
2
3
4
exclude(group: "ch.qos.logback", module: '*') // 排除spring自带的logback

implementation "org.slf4j:slf4j-api:1.7.25"
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.9.1"

配置log4j

resources下新建log4j.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" shutdownHook="disable">
<Properties>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="CONSOLE_LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN} [%10.10t] %-40.40c{1.}{cyan} :%m%n${LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
   /