本文介紹了Log4j2-版本2.5-如何使用java.util.properties對(duì)象配置log4j2的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試使用屬性對(duì)象配置log4j2版本2.5。這樣做的原因是從版本1.2.17遷移。我不能直接使用屬性文件。我們對(duì)它做了一些程序化的修改。
以下是我嘗試過的內(nèi)容:
LogTest.java
public class LogTest {
static {
System.setProperty("log4j.configurationFactory", "logsample.common.util.LogsampleConfigurationFactory");
}
private static Logger logger = LogManager.getLogger(LogTest.class.getName());
public static void main(String[] args) throws Exception
{
logger.debug("First log");
logger.info("Infoed");
}
}
跟蹤屬性
name = PropertiesConfig
property.filename = D:/rolling/rollingtest.log
appenders = file
appender.file.type = File
appender.file.name = LOGFile
appender.file.fileName = ${filename}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n
loggers = file
logger.file.name = logware.common.util
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFile
LogwareConfigurationFactory.java
public class LogsampleConfigurationFactory extends ConfigurationFactory {
@Override
protected String[] getSupportedTypes() {
return new String[]{".properties", "*"};
}
@Override
public Configuration getConfiguration(ConfigurationSource source) {
return new PropertiesConfiguration(createConfigurationSource(), null);
}
@Override
public Configuration getConfiguration(String name, URI configLocation) {
return new PropertiesConfiguration(createConfigurationSource(), null);
}
private ConfigurationSource createConfigurationSource()
{
Properties p = new Properties();
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = null;
try {
p.load(new FileInputStream("D:/log4jSample/properties/trace.properties"));
p.store(out, null);
} catch (IOException e) {
e.printStackTrace();
}
in = new ByteArrayInputStream(out.toByteArray());
ConfigurationSource configSrc = null;
try {
configSrc = new ConfigurationSource(in);
}
catch (IOException i)
{
}
return configSrc;
}
}
當(dāng)我運(yùn)行LogTest類時(shí),它無法獲取指針為空的LogContext。
異常堆棧
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration.<init>(BuiltConfiguration.java:58)
at org.apache.logging.log4j.core.config.properties.PropertiesConfiguration.<init>(PropertiesConfiguration.java:36)
at logware.common.util.LogwareConfigurationFactory.getConfiguration(LogwareConfigurationFactory.java:46)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:427)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:256)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:561)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:578)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:214)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:235)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:167)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:522)
at logware.common.util.LogTest.<clinit>(LogTest.java:58)
因此,我沒有將rootComponent放入PropertiesConfiguration構(gòu)造函數(shù)中。但我不知道它應(yīng)該是什么。
在這方面有任何指導(dǎo)或線索都是很好的。
推薦答案
對(duì)于2.5,我建議您這樣做:
@Override
public Configuration getConfiguration(ConfigurationSource source) {
PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
return factory.getConfiguration(source);
}
在最新版本中,您必須對(duì)其進(jìn)行修改才能執(zhí)行以下操作:
@Override
public Configuration getConfiguration(LoggerContext ctx, ConfigurationSource source) {
PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
return factory.getConfiguration(ctx, source);
}
這篇關(guān)于Log4j2-版本2.5-如何使用java.util.properties對(duì)象配置log4j2的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,