一 笔记
控制台console附加器
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="OFF" >
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[log4j2-demo] %-d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %c:%l - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
二 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wwx.log4j2</groupId>
<artifactId>005-log4j2-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
三 log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="OFF" >
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[log4j2-demo] %-d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %c:%l - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
四 Demo.java
package com.wwx.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Demo {
public static void main(String[] args) {
Logger logger = LogManager.getLogger(Demo.class);
logger.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal");
}
}
五 运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1794d043796fcaacae09874c1e746a36.png)
六 项目结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e027d148336fea25e0dc09a3fb3db413.png)