2016-10-04 41 views
1

プロパティオブジェクトを使用してlog4j2バージョン2.5を構成しようとしています。これを行う理由は、バージョン1.2.17からの移行です。私は直接プロパティファイルを使用することはできません。私たちはプログラム的にいくつかの変更を行います。ここでlog4j2 -version 2.5 - java.util.propertiesオブジェクトを使用してlog4j2を構成する方法

は、私が試したものです:

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"); 
    } 
} 

trace.properties

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; 
    } 
} 

私はLogTestを実行すると、クラスでは、nullポインタを持つ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) 

だから、私はPropertiesConfigurationコンストラクタに入れていないそのrootComponent。しかし、私はそれが何にすべきか分かりません。 これに関するガイダンスや手掛かりは素晴らしいでしょう。 2.5で

答えて

2

私はあなたが推薦する:

@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); 
} 
関連する問題