无法使用java.util.Properties配置Log4j2

无法使用java.util.Properties配置Log4j2,properties,configuration,log4j2,Properties,Configuration,Log4j2,我无法使用java.util.properties配置log4j2。我总是收到这样一条消息“tatusLogger没有找到Log4j 2配置文件”。请看我的日志。我正在从两个文件中读取log4j2属性 我将把我的代码附加到这篇文章 package my.common.logger; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream;

我无法使用java.util.properties配置log4j2。我总是收到这样一条消息“tatusLogger没有找到Log4j 2配置文件”。请看我的日志。我正在从两个文件中读取log4j2属性

我将把我的代码附加到这篇文章

package my.common.logger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
import org.apache.logging.log4j.util.PropertiesUtil;

public class MyLogger {
    private static boolean configured = false;
    private static Logger logger;
    static {
        System.setProperty("log4j.configurationFactory",     "my.common.logger.JCFLog4JConfigurationFactory");
    }
    private static void readConfiguration() throws Exception {
        LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
        Configuration configuration =  ConfigurationFactory.getInstance().getConfiguration(context, createConfigurationSource()); 
        configuration.start();
        Configurator.reconfigure(); 
        configured = true;
    }
    public static Logger getLogger(String className) {
        try {
            if (!configured) readConfiguration();           
            return org.apache.log4j.LogManager.getLogger(className);
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
        return null;
    }
    private static ConfigurationSource createConfigurationSource()
    {   Properties p = new Properties();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = null;
        try {
            p.putAll(<Read from File 1>);
            p.putAll(<Read from File 2>);
            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)
        {
            i.printStackTrace();
        }
        return configSrc;
    }
}
知道我做错了什么吗

谢谢
Nags

您是否尝试从命令行传递-Dlog4j.configuration={path to file}?如果有效,您可以考虑设置为静态上下文或通过环境变量,请改进您的问题。尽管MyLogger类中包含System.setProperty,但没有解释如何调用它。它不包含main方法,因此它不能作为应用程序的开始,并且可能Log4j正在其他地方初始化。您还没有解释为什么(不正确地)创建了自己的getLogger方法。
package nj.aoc.ito.cmfw.common.logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
public class MyLog4JConfigurationFactory extends ConfigurationFactory {
    public MyLog4JConfigurationFactory() {

    }
    @Override
    public Configuration getConfiguration(LoggerContext ctx, ConfigurationSource source) {

         PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
         return factory.getConfiguration(ctx, source);
    }
    @Override
    protected String[] getSupportedTypes() {
        return new String[]{".properties", "*"};
    }

}