2011-04-07 3 views
16

log4j를 사용하면 현재 DOMConfigurator 파일 인 log4j.xml의 이름과 경로가 무엇인지 알아낼 수 있습니다.이 파일의 이름과 경로가 필요한 PropertyConfigurator.configureAndWatch 메소드를 사용하여 파일 이름을 재설정합니다. 변경된 경우.log4j : 어떤 설정 파일이 사용 되었습니까?

API 문서에서는 config를 다시로드하도록 log4j를 구성하는 방법을 보여 주지만 자동으로 파일 이름과 경로를 볼 수있는 방법을 찾을 수 없습니다. 응용 프로그램이 모든 응용 프로그램 서버와 함께 독립 실행 형으로 실행 중입니다.

감사합니다.

답변

21

API에서 자동으로 선택되는 경로를 얻을 수있는 기회가없는 것 같습니다. log4j의 소스 코드를 이해 했으므로 탐지 된 경로는 사용되며 저장되지 않습니다.

은 적어도 당신은 시작에 출력의 log4j-내부 디버깅 정보에 -Dlog4j.debug 속성을 사용할 수 있습니다 그리고 당신은 몇 가지 정보를 다음과 같이 얻을 것이다 :

log4j: Trying to find [log4j.xml] using context classloader [email protected] 
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration. 
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator 

'log4j: Using URL ...' 라인은 LogManager 클래스에서 제공됩니다. 여기에서 초기화 프로세스를 확인할 수 있습니다. URL을 보면 나중에 정보가 저장되지 않습니다.

2

LogManager 클래스의 정적 초기화에서 log4j 사용과 동일한 프로세스를 사용할 수 있습니다. 다른 초기화 및 외부 구성을 알고 있어야합니다. 봄의 org.springframework.web.util.Log4jConfigListener에서.

public static URL getLog4jConfig() { 
    String override = OptionConverter.getSystemProperty("log4j.defaultInitOverride", null); 
    if (override == null || "false".equalsIgnoreCase(override)) { 
     String configurationOptionStr = OptionConverter.getSystemProperty("log4j.configuration", null); 

     URL url; 

     if (configurationOptionStr == null) { 
     url = Loader.getResource("log4j.xml"); 
     if (url == null) { 
      url = Loader.getResource("log4j.properties"); 
     } 
     } else { 
     try { 
      url = new URL(configurationOptionStr); 
     } catch (MalformedURLException ex) { 
      url = Loader.getResource(configurationOptionStr); 
     } 
     } 
     return url; 
    } else { 
     return null; 
    } 
    } 
관련 문제