Log4J的配置文件(Configuration File)就是用来设置记录器的级别、存放器和布局的,它可接key=value格式的设置或xml格式的设置信息。通过配置,可以创建出Log4J的运行环境。
1. 配置文件
Log4J配置文件的基本格式如下:
其中 [level] 是日志输出级别,共有5级:
为日志输出目的地,Log4j提供的appender有以下几种:
Layout:日志输出格式,Log4j提供的layout有以下几种:
Appender
打印参数: Log4J采用类似C语言中的printf函数的打印格式格式化日志信息,如下:
2. 在代码中初始化Logger:
1)在程序中调用BasicConfigurator.configure()方法:给根记录器增加一个ConsoleAppender,输出格式通过PatternLayout设为"%-4r [%t] %-5p %c %x - %m%n",还有根记录器的默认级别是Level.DEBUG.
2)配置放在文件里,通过命令行参数传递文件名字,通过PropertyConfigurator.configure(args[x])解析并配置;
3)配置放在文件里,通过环境变量传递文件名等信息,利用log4j默认的初始化过程解析并配置;
4)配置放在文件里,通过应用服务器配置传递文件名等信息,利用一个特殊的servlet来完成配置。
3. 为不同的 Appender 设置日志输出级别:
当调试系统时,我们往往注意的只是异常级别的日志输出,但是通常所有级别的输出都是放在一个文件里的,如果日志输出的级别是BUG!?那就慢慢去找吧。
这时我们也许会想要是能把异常信息单独输出到一个文件里该多好啊。当然可以,Log4j已经提供了这样的功能,我们只需要在配置中修改Appender的Threshold 就能实现,比如下面的例子:
[配置文件]
有了上面的配置文件后,可以通过以下任意一种方式对其进行装载:
- 让Log4j自动装载配置文件
将上面的文件命名为log4j.properties,并保存到WEB-INF/classes/ 目录下,这样当第一次调用Logger.getLogger(SomeClass.class.getName()); 时,Log4j 就会自动装载该配置文件对log 系统进行初始化。 - 用系统变量指定配置文件的位置
如果不想把配置文件放到WEB-INF/clases/ 目录下,或者不想把配置文件命名为log4j.properties 那么就可以通过系统变量log4j.configuration来对其进行自定义,例如:
-Dlog4j.configuration=file:/D:/projects/someproject/WEB-INF/log4j.properties
用指定位置的文件进行配置
-Dlog4j.configuration=foo.txt
用WEB-INF/classes/目录下的foo.txt 进行配置 - 用PropertyConfigurator 读取配置文件
import org.apache.log4j.Logger;
......
public class Log4jTestServlet {
private static Logger logger = Logger.getLogger(Log4jTestServlet.class.getName());
......
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String file = getRealPath(getInitParameter("log4j-init-file"));
PropertyConfigurator.configure(file);
/*
*PropertyConfigurator.configure(Properties);
*PropertyConfigurator.configure(URL);
*PropertyConfigurator.configureAndWatch(file, 30); //delay 30 second
*/
logger.info("log4j has been configurated with PropertyConfigurator.configuration successfully!");
}
}
###############################################################
# documenation of org.apache.log4j.PropertyConfigurator.
# The root category uses the appender called A1. Since no priority is
# specified, the root category assumes the default priority for root
# which is DEBUG in log4j. The root category is the only category that
# has a default priority. All other categories need not be assigned a
# priority in which case they inherit their priority from the
# hierarchy.
# "factor.log". Start the server NumberCruncherServer and two
# NumberCruncherClients, and ask to factor two numbers
# near-simultaneously. Notice that the log output from these two
# requests are logged in the file factor.log. Nevertheless, the logs
# of these requests can still be distinguished given their distinct
# nested diagnostic contexts.
log4j.appender.A1.File=Log4J.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
# %d date time
# %-5p debug level
# %m messages
# %l class with method and line number (slowly! dubug only, on release use %c{2} in release version)
# %n /n or /r/n
log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %l - %m%n
log4j.appender.A2.layout.ConversionPattern=%d [%-5p] %l - %m%n
# log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %c{2} - %m%n
private static final Logger logger = Logger.getLogger(TestLog4J.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
logger.info("This is a Log4J info Testing!");
logger.error("This is a Log4J error Testing!");
//The debug log will be not printed on the text file
//and the console, because the log4j's rootCategory
//is INFO, so there are just INFO WARN ERROR JATAL can be
//printed on the text file and the console.
logger.debug("This is a Log4J Debug Testing!");
}