pom文件
java
<!--log4j2的依赖-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.23.1</version>
</dependency>
log4j2.xml
java
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<loggers>
<root level="DEBUG">
<appender-ref ref="spring6log"/>
</root>
</loggers>
<appenders>
<console name="spring6log" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger-{1024} - %msg%n"/>
</console>
</appenders>
</configuration>
pattern定义日志输出格式:
配置 说明
%d{HH:mm:ss.SSS} 输出日志打印的时间,精确到毫秒
%t\] 输出当前线程名称
%-5level 输出日志级别,-5表示左对齐并且固定输出5个字符,不足在右边补空格
%logger{36} 输出logger名称,一般是类的全限定路径名
%msg 输出要打印的业务日志信息
%n 日志换行
实际会输出如下日志:
```java
11:33:08.438 [main] ERROR org.apache.logging.log4j.Log4j2Test - error level log
```
Log4j2共有8种日志级别,
按照优先级从小到大排序:
```java
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
```
```java