commons-logging和log4j属性文件 [英] commons-logging and log4j properties file

查看:64
本文介绍了commons-logging和log4j属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过commons-logging使用log4j,如果log4j属性文件未称为log4.properties,则会遇到问题. 我收到以下错误: log4j:WARN找不到记录器(LogMePlease)的附加程序. log4j:WARN请正确初始化log4j系统.

I am trying to use log4j via commons-logging and having problems if the log4j properties file is not called log4.properties. I get following error: log4j:WARN No appenders could be found for logger (LogMePlease). log4j:WARN Please initialize the log4j system properly.

我的代码很简单:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogMePlease 
{
static Log l = LogFactory.getLog(LogMePlease.class);

public static void main(String [] args)
{
    l.warn("Hello World!");
}
}

在我的课堂上,我有: commons-logging.properties 文件,其中包含以下条目

In my class path, i have: commons-logging.properties file which contains following entries

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.configuration=log4j-test.properties

log4j-test.properties 文件

运行此代码时,我会得到

when i run this code i get

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

如果我将log4j-test.properties文件重命名为log4j.properties,则一切正常. 因此,问题是如何设置公共日志以对log4j.properties文件使用任意名称.

If I rename log4j-test.properties file to be log4j.properties - then everything works. So, the question is how can I setup commons logging to use arbitrary name for log4j.properties file.

推荐答案

仅从公共日志中读取文件commons-logging.properties,而log4j将在系统属性中查找log4j.configuration.

The file commons-logging.properties is only read from commons logging while log4j will look for log4j.configuration in the system properties.

因此,您必须在命令行上使用-Dlog4j.configuration=log4j-test.properties作为JVM选项来指定它们,或者必须在第一次调用任何日志记录方法之前调用System.setProperty()(通常很难实现).

So you must either specify them with -Dlog4j.configuration=log4j-test.properties on the command line as a JVM option or you must call System.setProperty() before the first call to any logging method (which is usually pretty hard to achieve).

注意:如果可以,请使用XML配置log4j.xml;配置log4j更加简单和强大.

Note: If you can, use the XML config log4j.xml; it's much more simple and powerful for configuring log4j.

这篇关于commons-logging和log4j属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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