Log4j2 简单配置
1. 下载log4j2
http://logging.apache.org/log4j/2.x/download.html
1.1 在工程中添加如下两个jar包
log4j-api-2.0-beta9.jar
log4j-core-2.0-beta9.jar
2. 在工程src目录下面建立建立配置文件 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<Filter type="ThresholdFilter" level="trace"/>
<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="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
3. 实例代码如下
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test {
private static Logger logger= LogManager.getLogger("Test");
public static void main(String[] args) {
//System.setProperty("log4j.configurationFile", "log4j2.xml");
logger.info("Hello World!");
logger.error("Error Test");
// TODO Auto-generated method stub
}
}