使用默认配置
默认配置为输出到控制台,且日志基本为ERROR.
public void testUseDefaultConf() {
Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
logger.trace("trace level");
logger.debug("debug level");
logger.info("info level");
logger.warn("warn level");
logger.error("error level");
logger.fatal("fatal level");
}
其输出结果:
16:59:42.902 [main] ERROR - error level
16:59:42.906 [main] FATAL - fatal level
自定义配置文件位置
用Configurator执行初始化:
public class LoadLog4jConf {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("log4j2/log4j2.xml");
ConfigurationSource source = new ConfigurationSource(resourceAsStream);
Configurator.initialize(null, source);
Logger logger = LogManager.getLogger(LoadLog4jConf.class);
logger.info("LoadLog4jConf");
}
}