Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
log4j2-版本2.5-如何使用java.util.properties对象配置log4j2_Java_Log4j_Log4j2 - Fatal编程技术网

log4j2-版本2.5-如何使用java.util.properties对象配置log4j2

log4j2-版本2.5-如何使用java.util.properties对象配置log4j2,java,log4j,log4j2,Java,Log4j,Log4j2,我正在尝试使用properties对象配置log4j2版本2.5。这样做的原因是从版本1.2.17进行迁移。我不能直接使用属性文件。我们在程序上对它做了一些修改 以下是我尝试过的: LogTest.java trace.properties LogwareConfigurationFactory.java 当我运行LogTest类时,它无法获取带有空指针的LogContext 异常堆栈 java.lang.ExceptionInInitializeError 原因:java.lang.NullP

我正在尝试使用properties对象配置log4j2版本2.5。这样做的原因是从版本1.2.17进行迁移。我不能直接使用属性文件。我们在程序上对它做了一些修改

以下是我尝试过的:

LogTest.java trace.properties LogwareConfigurationFactory.java 当我运行LogTest类时,它无法获取带有空指针的LogContext

异常堆栈
java.lang.ExceptionInInitializeError
原因:java.lang.NullPointerException
位于org.apache.logging.log4j.core.config.builder.impl.BuilConfiguration.(BuilConfiguration.java:58)
位于org.apache.logging.log4j.core.config.properties.PropertiesConfiguration.(PropertiesConfiguration.java:36)
在logware.common.util.LogwareConfigurationFactory.getConfiguration(LogwareConfigurationFactory.java:46)中
位于org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:427)
位于org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:256)
位于org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:561)
位于org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:578)
位于org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:214)
位于org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:235)
位于org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
位于org.apache.logging.log4j.LogManager.getContext(LogManager.java:167)
位于org.apache.logging.log4j.LogManager.getLogger(LogManager.java:522)
在logware.common.util.LogTest.(LogTest.java:58)
因此,它是我没有放在PropertiesConfiguration构造函数中的根组件。但我不知道应该是什么。
这方面的任何指导或线索都很好。

对于2.5,我建议您:

@Override
public Configuration getConfiguration(ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(source);
}
对于最新版本,您必须对此进行修改才能执行以下操作:

@Override
public Configuration getConfiguration(LoggerContext ctx, ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(ctx, source);
}
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;
    }
}
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)
@Override
public Configuration getConfiguration(ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(source);
}
@Override
public Configuration getConfiguration(LoggerContext ctx, ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(ctx, source);
}