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

本文是小编为大家收集整理的关于commons-logging和log4j属性文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图通过commons-loging使用log4j,如果log4j属性文件未称为log4.properties,则有问题. 我会有以下错误: log4j:警告logger(logMeplease)找不到附录. log4j:警告请正确初始化log4j系统.

我的代码非常简单:

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 包含以下条目的文件

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

和 log4j-test.properties 文件

当我运行此代码时,我会得到

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

如果我将log4j -test.properties文件重命名为log4j.properties-那么一切都起作用. 因此,问题是如何设置Commons记录以使用log4j.properties文件使用任意名称.

推荐答案

文件commons-logging.properties仅从Commons记录中读取,而Log4j将在系统属性中寻找log4j.configuration.

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

注意:如果可以,请使用XML Config log4j.xml;配置log4j.

更简单和功能更强大

其他推荐答案

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

没有协议,它将在类路径中查看.

其他推荐答案

>,喜欢您的情况.

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

设置log4j.properties,在log.

的实例之前
System.setProperty("log4j.configuration", "log4j.properties");

本文地址:https://www.itbaoku.cn/post/1574872.html

问题描述

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.

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

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

and log4j-test.properties file

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.

推荐答案

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

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

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

其他推荐答案

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

Without the protocol it will look in the classpath.

其他推荐答案

jul - commons-logging, it likes your situation.

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.

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