<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="INFO" monitorInterval="30">
<!--先定义所有的appender-->
<appenders>
<!--这个输出控制台的配置-->
<console name="Console" target="SYSTEM_OUT">
<!--输出日志的格式-->
<PatternLayout pattern="[ %p ] [%-d{yyyy-MM-dd HH:mm:ss}] [ LOGID:%X{logid} ] [%l] %m%n"/>
</console>
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
<File name="Test" fileName="log/test.log" append="false">
<PatternLayout pattern="[ %p ] %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] [%l] %m%n"/>
</File>
<RollingFile name="RollingFileInfo" fileName="log/log.log" filePattern="log/log.log.%d{yyyy-MM-dd}">
<!-- 只接受level=INFO以上的日志 -->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[ %p ] [%-d{yyyy-MM-dd HH:mm:ss}] [ LOGID:%X{logid} ] [%l] %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
<SizeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileError" fileName="log/error.log" filePattern="log/error.log.%d{yyyy-MM-dd}">
<!-- 只接受level=WARN以上的日志 -->
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<PatternLayout pattern="[ %p ] %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] [%l] %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
<SizeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</appenders>
<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
<loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<root level="all">
<appender-ref ref="Console"/>
<appender-ref ref="Test"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
运行时指定上述log4j2.xml
的配置:
-Dlog4j.configurationFile=./conf/log4j2.xml
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
/**
* Created by think on 2017/1/22 0022.
*/
public class LogTest {
private static final Logger logger = LogManager.getLogger(LogTest.class);
static class test extends Thread {
@Override
public void run() {
long time = System.currentTimeMillis();
ThreadContext.put("logid", String.valueOf( time));
logger.info("test thread.");
ThreadContext.clearAll();
}
}
public static void main(String[] args) {
long time = System.currentTimeMillis() ;
ThreadContext.put("logid", String.valueOf(time));
try {
logger.trace("trace...");
logger.debug("debug...");
logger.info("info...");
logger.warn("warn...");
logger.error("error...");
logger.fatal("fatal...");
test test1 = new test();
test test2 = new test();
test1.start();
test2.start();
} catch (Exception e) {
e.printStackTrace();
}
ThreadContext.clearAll();
}
}
输出:
[ TRACE ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:37)] trace...
[ DEBUG ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:38)] debug...
[ INFO ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:39)] info...
[ WARN ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:40)] warn...
[ ERROR ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:41)] error...
[ FATAL ] [2017-02-04 17:10:27] [ LOGID:199427158 ] [logtest.LogTest.main(LogTest.java:42)] fatal...
[ INFO ] [2017-02-04 17:11:38] [ LOGID:199498552 ] [logtest.LogTest$test.run(LogTest.java:20)] test thread.
[ INFO ] [2017-02-04 17:11:38] [ LOGID:199498551 ] [logtest.LogTest$test.run(LogTest.java:20)] test thread.