1. 在pom.xml加入log4j2的依赖
- <dependencies>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-api</artifactId>
- <version>2.0-rc1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.0-rc1</version>
- </dependency>
- </dependencies>
2. 引入log4j2的jar包
log4j2的包很多, 引入两个jar包就可以了,log4j-api-2.0-rc1.jar和log4j-core-2.0-rc1.jar
3. 使用log4j2
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- private static Logger logger = LogManager.getLogger("HelloWorld");
- logger.error("Hello, World!");
4. 修改log4j2的默认配置
- <?xml version="1.0" encoding="UTF-8"?>
- <Configuration status="WARN">
- <Appenders>
- <Console name="Console" target="SYSTEM_OUT">
- <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
- </Console>
- </Appenders>
- <Loggers>
- <Root level="trace"> // 这儿为trace表示什么都可以打印出来了,其他几个级别分别为:TRACE、DEBUG、INFO、WARN、ERROR和FATAL
- <AppenderRef ref="Console"/>
- </Root>
- </Loggers>
- </Configuration>