log4j2 + slf4j

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta9</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.0-beta9</version>
</dependency>

LOF4J2配置文件现在改为XML格式或者JSON格式.以前的Properties配置文件不再支持

以下附上一个简单的配置文件.文件名为log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>  
<configuration status="DEBUG">  
    <appenders>  
        <Console name="consolePrint" target="SYSTEM_OUT">  
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />  
        </Console>
        <File name="File" fileName="logs/app.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </appenders>  
    <loggers>  
        <root level="info">  
            <appender-ref ref="consolePrint" /> 
            <appender-ref ref="File" /> 
        </root>  
    </loggers>  
</configuration>

java代码中依然使用SLF4J来管理所有的日志

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final Logger log = LoggerFactory.getLogger(Test.class);


你可能感兴趣的:(log4j2 + slf4j)