Java commons-logging 和 log4j 属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1524109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:47:16  来源:igfitidea点击:

commons-logging and log4j properties file

javalog4japache-commons-logging

提问by yasha

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

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 文件使用任意名称。

回答by Aaron Digulla

The file commons-logging.propertiesis only read from commons logging while log4j will look for log4j.configurationin the system properties.

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

So you must either specify them with -Dlog4j.configuration=log4j-test.propertieson 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 更加简单和强大。

回答by aerobiotic

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

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

Without the protocol it will look in the classpath.

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

回答by zg_spring

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