简体   繁体   English

使用slf4j-log4j12使用Log4j设置配置文件

[英]Setting configuration file with Log4j using slf4j-log4j12

I know there is several answers for how to set the file. 我知道如何设置文件有几个答案。 Non have worked with me. 非与我合作。

I'm trying to unifying several conf files for that I need to set the conf of log4j provided in slf4j-log4j12 into the configuration file provided as parameter of the program. 我正在尝试统一几个conf文件,我需要将slf4j-log4j12中提供的log4j的conf设置为作为程序参数提供的配置文件。 Till now I tried by setting as parameter of the jvm like: 直到现在我尝试通过设置为jvm的参数,如:

java '-Dlog4j.configuration=conf.cfg' -jar my.jar

I'm getting back: 我回来了:

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:ERROR Could not parse file [].

Setting programatically by: 以编程方式设置:

 System.setProperty("log4j.configuration",(new File(  Configurator.getDefaultConfig().getString(Const.LoggingDefaultLoggingFile))).toURL().toString());

and

 System.setProperty("log4j.configurationFile",(new File(  Configurator.getDefaultConfig().getString(Const.LoggingDefaultLoggingFile))).toURL().toString());

Same result. 结果相同。

I tried using DOMConfigurator but is for xml I'm using properties so no success. 我尝试使用DOMConfigurator但是对于xml我正在使用properties所以没有成功。

I was searching how to reload the log4j but the give complex solution for making subscription for monitoring change and constant loading of changes. 我正在搜索如何重新加载log4j,但提供了复杂的解决方案,用于订阅监视更改和不断加载更改。

I just want programatically change the configuration file once . 我只想以编程方式更改一次配置文件。

Till now, I was able to set the file just if I put it directly into the jar. 直到现在,我才能将文件设置为直接放入jar中。 But this by using a separated file with the default name. 但这是通过使用具有默认名称的分隔文件。

Is there a way of loading the file directly int the Log4j (no shity system properties)? 有没有一种方法可以将文件直接加载到Log4j中(没有shity系统属性)? o there is a simple way of doing this better? o有一个简单的方法可以更好地做到这一点?

  1. log4j looking for log4j.properties under WEB-INF\\classes log4j在WEB-INF\\classes下查找log4j.properties

  2. PropertyConfigurator class, PropertyConfigurator类,

example of usage: 使用示例:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
// ...
public static final Logger log=Logger.getLogger(App.class);
// ...
PropertyConfigurator.configure("log4j.properties");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM