commons-logging和log4j属性文件

时间:2021-10-01 00:35:24

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.

我试图通过commons-logging使用log4j,如果log4j属性文件没有被称为log4.properties则会遇到问题。我收到以下错误:log4j:WARN没有找到记录器(LogMePlease)的appender。 log4j:WARN请正确初始化log4j系统。

My code is very simple:

我的代码非常简单:

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!");
}
}

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

在我的类路径中,我有:commons-logging.properties文件,其中包含以下条目

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

and log4j-test.properties file

和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.

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.

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

3 个解决方案

#1


10  

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

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

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).

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

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

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

#2


1  

You need to add the protocol to the front of System property value like so: -Dlog4j.configuration=file://log4j-test.properties

您需要将协议添加到System属性值的前面,如下所示:-Dlog4j.configuration = file://log4j-test.properties

Without the protocol it will look in the classpath.

如果没有协议,它将在类路径中查找。

#3


0  

jul - commons-logging, it likes your situation.

jul - commons-logging,它喜欢你的情况。

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

set log4j.properties, before instance of Log.

在Log的实例之前设置log4j.properties。

System.setProperty("log4j.configuration", "log4j.properties");

#1


10  

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

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

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).

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

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

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

#2


1  

You need to add the protocol to the front of System property value like so: -Dlog4j.configuration=file://log4j-test.properties

您需要将协议添加到System属性值的前面,如下所示:-Dlog4j.configuration = file://log4j-test.properties

Without the protocol it will look in the classpath.

如果没有协议,它将在类路径中查找。

#3


0  

jul - commons-logging, it likes your situation.

jul - commons-logging,它喜欢你的情况。

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

set log4j.properties, before instance of Log.

在Log的实例之前设置log4j.properties。

System.setProperty("log4j.configuration", "log4j.properties");