2016-10-04 3 views
0

log4j2는 여러 구성 파일을 사용할 수 있습니다. 내 프로젝트를 실행하고 하나의 기본 설정 파일 인 logger.xml을로드하고 나서 다른 파일 logger_1.xml의 두 번째 구성이 있는지 확인하고 추가하고 첫 번째 설정을 덮어 쓰지 않으려 고합니다. 여기에 몇 가지 더미 코드가 있습니다. 간단히 말해서 arrayList를 파일 경로로 채우고 모두로드하려고합니다.LOG4J2 Java를 사용하는 다중 구성 파일 사용

public class LoggerConfiguratorManager 
{ 
public static final String LOG4J_PATH = "etc/confs/logger.xml"; 

    private static LoggerContext context = null; 
    private static final ConfigurationFactory factory = XmlConfigurationFactory.getInstance(); 
    private static ConfigurationSource configurationSource = null; 
    private static Configuration configuration = null; 
    private static final ArrayList<String> registred_logger = new ArrayList<>(); 

    private static void loadLoggerConfig(String logger_path) 
    { 
    InputStream is = null; 
    try 
    { 
     if(logger_path.endsWith(".xml")) 
     is = new FileInputStream(logger_path); 
     else 
     { 
     final ZipFile archive = new ZipFile(logger_path); 
     final ZipEntry logger_entry = archive.getEntry(LOG4J_PATH); 
     if(logger_entry == null) throw new IOException("Cannot find 'logger.xml' in " + logger_path); 
     is = archive.getInputStream(logger_entry); 
     } 

     configurationSource = new ConfigurationSource(is); 
     configuration = factory.getConfiguration(configurationSource); 
    } 
    catch(IOException ex) 
    { 
     System.err.println("============================================================================="); 
     System.err.println("=============================== LOGGER CONFIG ==============================="); 
     System.err.println("============================================================================="); 
     System.err.println("=== [ERROR] " + ex); 
    } 
    finally 
    { 
     if (configurationSource != null) 
     { 
     context = Configurator.initialize(null, configurationSource); 
     context.start(configuration); 
     try { is.close(); } catch(IOException ex) { } 
     } 
    } 
    } 

    public static void load() 
    { 
    registred_logger.add(Globals.getClassLocation(LoggerConfiguratorManager.class)); 

    for(final String conf : registred_logger) 
     loadLoggerConfig(conf); 
    } 

    public static void regLoggerConf(String conf_path) { registred_logger.add(conf_path); } 

답변

0

내가 대신 일을 제안 : 전체 작업 예를 참조하십시오 https://github.com/rgoers/CompositeConfigurationExample를 들어

public class LoggerConfiguratorManager { 

    private static final String LOG4J_PATH = "etc/confs/log4j2.xml"; 
    private static final StringBuffer paths = new StringBuffer(LOG4J_PATH); 

    public static void registerConfiguration(String confPath) { 
     paths.append(",").append(confPath); 
    } 

    public static void initLog4j() { 
     Configurator.initializer("My Config", null, paths.toString(), null); 
    } 
    } 

합니다.

+0

내 경우에는 파일이 다른 프로그램 내부에 있습니다. 즉, jar 아카이브 개미 안이 나와 다른 도움이되지 않습니다. (다른 제안. –

+0

어떻게 파일을 찾으십니까? – rgoers

+0

내 코드를 볼 수 있습니다. 먼저 현재 클래스 파일의 경로를 얻은 다음 ZipEntry를 사용하여 전체 logger.xml을 InputStream으로 가져옵니다 –

관련 문제