Log4j版本:2.3
操作系统:Ubuntu 13.04
JDK版本:1.7.0_75
小例子:
在Main类中,使用log4j日志功能。
package main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
public static void main(String args[]){
Logger Log = LogManager.getLogger(Main.class.getName());
Log.info("this is the info");
Log.warn("this is the warn info");
Log.error("this is the error info");
Log.fatal("this is the fatal info");
Log.trace("enter Main.test()");
//new Main().test();
Log.trace("exit Main.test()");
}
}
如果我直接这样运行的话,输出结果为:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
22:22:01.774 [main] ERROR main.Main - this is the error info
22:22:01.775 [main] FATAL main.Main - this is the fatal info
最上面是一条错误信息,说没有log4j2配置文件,然后使用了默认的配置,日志仅仅把error及以上级别的信息输出到控制台。
然后在官网上发现,log4j2配置有四种方式
http://logging.apache.org/log4j/2.x/manual/configuration.html
:
- Through a configuration file written in XML, JSON, or YAML.
- Programmatically, by creating a ConfigurationFactory and Configuration implementation.
- Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
- Programmatically, by calling methods on the internal Logger class.
我使用的是第一种方法,即配置log4j2.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <!-- 这个status是控制系统信息的输出级别 -->
<Appenders>
<Console name="Console" target="SYSTEM_