一、添加maven依赖

<!-- 使用aliyun镜像 -->
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories> <dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>

二、log4j2配置文件log4j2.xml(亦可使用对应节点属性配置参考官网地址)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<properties>
<Property name="console-pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%level] %logger{36} - %msg%n</Property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${console-pattern}" />
</Console>
</Appenders> <Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

三、测试代码

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; /**
* @author : zhangyanqing
* @time : 2017/12/9
* @desc : 根控制器日志测试
*/
public class ConsoleLoggerTest {
public static final Logger LOGGER = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
public static void main(String[] args){
LOGGER.trace("trace level");
LOGGER.debug("debug level");
LOGGER.info("info level");
LOGGER.warn("warn level");
LOGGER.error("error level");
LOGGER.fatal("fatal level"); }
}
05-11 17:45