如何正确初始化log4j? [英] How to initialize log4j properly?

查看:267
本文介绍了如何正确初始化log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将log4j添加到我的应用程序后,我每次执行我的应用程序时都会得到以下输出:

After adding log4j to my application I get the following output every time I execute my application:


log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

这似乎意味着缺少一个配置文件。
这个配置文件应该放在哪里,什么是好的开始内容?

It seems this means a configuration file is missing. Where should this config file be located and what is a good start content?

我使用纯java开发桌面应用程序。所以没有webserver等...

I'm using plain java for developing a desktop application. So no webserver etc...

推荐答案

Log4j默认在类路径中寻找一个名为log4j.properties或log4j.xml的文件。
您可以通过设置系统属性来控制要使用哪个文件进行初始化,如所述这里(查找默认初始化过程部分)。

Log4j by default looks for a file called log4j.properties or log4j.xml on the classpath. You can control which file it uses to initialize itself by setting system properties as described here (Look for the "Default Initialization Procedure" section).

例如:

java -Dlog4j.configuration=customName ....

将导致log4j在类路径上查找名为customName的文件。

Will cause log4j to look for a file called customName on the classpath.

如果你有问题,我打开log4j.debug是有帮助的:

If you are having problems I find it helpful to turn on the log4j.debug:

-Dlog4j.debug

它将打印到System.out许多有用的信息,用于初始化自身,记录器/放置器配置以及如何配置等。

It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

配置文件可以是java属性文件或xml文件。以下是来自 log4j简介文档页面的属性文件格式示例:

The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

这篇关于如何正确初始化log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆