我下载的是”apache-log4j-2.0-beta9-bin.zip”,解压该文件,将log4j-api-2.0-beta9.jar、log4j-core-2.0-beta9.jar导入项目,即可使用log4j 2
2. 初识log4j 2
package log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test01 {
public static void main(String[] args) {
Logger logger = LogManager.getLogger(Test01.class.getName());
logger.trace(trace);
logger.debug(debug);
logger.info(hello);
logger.warn(warn);
logger.error(error);
logger.fatal(fatal);
}
}
Output:
12:24:25.398 [main] ERROR log4j2.Test01 - error 12:24:25.401 [main] FATAL log4j2.Test01 - fatal
代码中写了6种输出,为什么只出来”error”,”fatal”呢?
原来log4j 2初始化时,自动从src目录加载配置文件。配置文件的格式有两种:json和xml
配置文件的优先级(从高到低):
log4j2-test.json(或log4j2-test.jsn)
log4j2-test.xml
log4j2.json(或log4j2.jsn)
log4j2.xml
默认配置文件
log4j 2初始化时,优先从src目录下找”log4j2-test.json(或log4j2-test.jsn)”,如果没找到,会继续找”log4j2-test.xml”,以此类推
刚才的例子中,src目录下没有配置文件,因此log4j 2将使用默认配置文件
默认配置文件由DefaultConfiguration类提供。DefaultConfiguration内容:
* Licensed to the Apache Software Foundation (ASF) under one or more
package org.apache.logging.log4j.core.config;
import java.io.Serializable;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.util.PropertiesUtil;
/**
* The default configuration writes all output to the Console using the default logging level. You configure default
* logging level by setting the system property org.apache.logging.log4j.level to a level name. If you do not
* specify the property, Log4j uses the ERROR Level. Log Events will be printed using the basic formatting provided
* by each Message.
*/
public class DefaultConfiguration extends BaseConfiguration {
/**
* The name of the default configuration.
*/
public static final String DEFAULT_NAME = Default;
/**
* The System Property used to specify the logging level.
*/
public static final String DEFAULT_LEVEL = org.apache.logging.log4j.level;
/**
* Constructor to create the default configuration.
*/
public DefaultConfiguration() {
setName(DEFAULT_NAME);
final Layout
layout =
PatternLayout.createLayout(%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n, null, null, null, null);
final Appender appender =
ConsoleAppender.createAppender(layout, null, SYSTEM_OUT, Console, false, true);
appender.start();
addAppender(appender);
final LoggerConfig root = getRootLogger();
root.addAppender(appender, null, null);
final String levelName = PropertiesUtil.getProperties().getStringProperty(DEFAULT_LEVEL);
final Level level = levelName != null && Level.valueOf(levelName) != null
Level.valueOf(levelName) : Level.ERROR;
root.setLevel(level);
}
@Override
protected void doConfigure() {
}
}
DefaultConfiguration提供的默认配置等同于如下配置:
log4j 2定义了8个事件级别:ALL,TRACE, DEBUG, INFO, WARN, ERROR ,FATAL,OFF(由低到高)
由于Root level设置为”error”,因此只有等于或高于”error”级别的事件才能输出。所以刚才的例子中只输出了”error”,”fatal”
3. log4j 2读取配置文件
log4j 2读取的配置文件可以分为三类:src下的配置文件、绝对路径的配置文件、相对路径的配置文件
3.1 log4j 2读取src下的配置文件
在src目录下创建log4j2.xml,复制log4j 2默认配置的内容到log4j2