복붙노트

[SPRING] Log4J를 봄으로 초기화 하시겠습니까?

SPRING

Log4J를 봄으로 초기화 하시겠습니까?

내 Log4J 로그 팩토리를 초기화하기 위해 Spring의 Log4jConfigurer 클래스를 사용하는 웹 응용 프로그램이 있습니다. 기본적으로 Log4J는 클래스 경로를 벗어나는 구성 파일로 초기화합니다.

설정은 다음과 같습니다.

<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>#{ MyAppHome + '/conf/log4j.xml'}</value>
        </list>
    </property>
</bean>

그러나 응용 프로그램 시작시이 오류가 발생합니다.

log4j : WARN 로거에 대한 어펜더를 찾을 수 없습니다.

수많은 애플리케이션 컨텍스트 초기화 메시지가 콘솔에 출력된다. 내 로거를 초기화 할 기회가 있기 전에 Spring이 내 응용 프로그램을 초기화하는 작업을하기 때문에 이것이라고 생각합니다. 문제가 발생하면 Log4J 위에 SLF4J를 사용하고 있습니다.

Log4jConfigurer를 초기화 된 첫 번째 bean으로 만들 수있는 방법이 있습니까? 아니면 이것을 해결할 다른 방법이 있습니까?

해결법

  1. ==============================

    1.spring-context.xml 대신 web.xml에서 Log4j 수신기를 구성 할 수 있습니다.

    spring-context.xml 대신 web.xml에서 Log4j 수신기를 구성 할 수 있습니다.

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.web.properties</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    

    그래서 봄이 시작되기 전에 일어났습니다.

  2. ==============================

    2.우리의 독립형 애플리케이션은 email config가 이미 Spring 설정 파일에 존재하고 우리가 log4j.properties에 복사하지 않기를 원하는 SMTPAppender를 필요로합니다.

    우리의 독립형 애플리케이션은 email config가 이미 Spring 설정 파일에 존재하고 우리가 log4j.properties에 복사하지 않기를 원하는 SMTPAppender를 필요로합니다.

    다음과 같이 봄을 사용하여 추가 appender를 추가했습니다.

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <bean factory-method="getRootLogger"
                  class="org.apache.log4j.Logger" />
        </property>
        <property name="targetMethod">
            <value>addAppender</value>
        </property>
        <property name="arguments">
            <list>
                <bean init-method="activateOptions"
                      class="org.apache.log4j.net.SMTPAppender">
                    <property name="SMTPHost" ref="mailServer" />
                    <property name="from" ref="mailFrom" />
                    <property name="to" ref="mailTo" />
                    <property name="subject" ref="mailSubject" />
                    <property value="10" name="bufferSize" />
                    <property name="layout">
                        <bean class="org.apache.log4j.PatternLayout">
                            <constructor-arg>
                                <value>%d, [%5p] [%t] [%c] - %m%n</value>
                            </constructor-arg>
                        </bean>
                    </property>
                    <property name="threshold">
                        <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"
                              id="org.apache.log4j.Priority.ERROR" />
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    

    classpath에 log4j.properties 파일도 있습니다.이 파일은 정규 FileAppenders를 자세히 설명합니다.

    나는 이것이 당신이 필요로하는 것에 대해 과잉이라고 생각합니다. :)

  3. ==============================

    3.log4j를 코드로 직접 설정하는 대신 log4j를 (사용자 정의) 구성 파일의 위치에 추가하여

    log4j를 코드로 직접 설정하는 대신 log4j를 (사용자 정의) 구성 파일의 위치에 추가하여

    -Dlog4j.configuration=.../conf/log4j.xml
    

    서버의 시작 등록 정보에?

    더 좋은 점은, log4j.xml을 클래스 패스의 기본 위치로 옮기고 log4j가 자동으로 구성되도록하십시오.

  4. ==============================

    4.하드 코드 된 경로 대신 classpath를 사용할 수 있습니다. 그것은 나를 위해 일한다.

    하드 코드 된 경로 대신 classpath를 사용할 수 있습니다. 그것은 나를 위해 일한다.

    <bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>classpath:/conf/log4j.xml</value>
            </list>
        </property>
    </bean>
    
  5. ==============================

    5.Jetty를 사용하는 경우 애플리케이션별로 추가 클래스 경로를 추가 할 수 있습니다.

    Jetty를 사용하는 경우 애플리케이션별로 추가 클래스 경로를 추가 할 수 있습니다.

    http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading#Adding_Extra_Classpaths_to_Jetty

    이렇게하면 log4 속성을 표준 방식으로로드 할 수 있습니다 (classpath에서 :).

    web.xml에서 :

           <listener>
               <listener-class>org.springframework.web.util.Log4jWebConfigurer</listener-class>
           </listener>
           <context-param>
               <param-name>log4jConfigLocation</param-name>
               <param-value>classpath:${project.artifactId}-log4j.properties</param-value>
           </context-param>
    

    jetty-web.xml :

            <Set name="extraClasspath">
                <SystemProperty name="config.home" default="."/>/contexts/log4j
            </Set>
    
  6. from https://stackoverflow.com/questions/4400583/initializing-log4j-with-spring by cc-by-sa and MIT license