2014-04-07 5 views
0

로거 인스턴스를 스프링과 함께 추가 할 수 있습니까? 그리고 각 사용자 정의 코드에서 각 메소드 호출을 추적하는 방법이 있습니까?로거 추가 스프링

나는 보통 이렇게 :

package my.java.code; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
public class A { 
    // How to add this line with Spring ? 
    private static final Logger logger = LoggerFactory.getLogger(A.class); 
    public void A() { 
    // How to add this line with Spring ? 
    logger.trace(""); 
    // Do something... 
    } 
    public void A(Object o) { 
    // How to add this line with Spring ? 
    logger.trace("{}", o); 
    // Do something... 
    } 
    public void method1() { 
    // How to add this line with Spring ? 
    logger.trace(""); 
    // Do something... 
    } 
    public void method2(Object o) { 
    // How to add this line with Spring ? 
    logger.trace("{}", o); 
    // Do something... 
    } 
} 

봄과 함께이 문제를 단순화 할 수있는 방법이 있나요?

목표는 다음과 같습니다

  • 피하기 repeatitive 코드
+0

나는이 질문을 이해하지 못한다. 네가 가진 것에 뭐가 문제 야? –

+1

중복 된 코드를 제거 하시겠습니까? – geoand

+2

Spring AOP와 ['CustomizableTraceInterceptor'] (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/aop/interceptor/CustomizableTraceInterceptor.html)를 사용하십시오. http://stackoverflow.com/questions/13182205/java-spring-aop-using-customizabletraceinterceptor-with-javaconfig-enableaspec을 참조하십시오. –

답변

1
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <bean id="customizableTraceInterceptor" 
    class="org.springframework.aop.interceptor.CustomizableTraceInterceptor"> 
    <property name="enterMessage" 
     value="Entering $[targetClassName].$[methodName]($[argumentTypes] $[arguments])" /> 
    <property name="exitMessage" value="Leaving $[methodName](): $[returnValue]" /> 
    </bean> 

    <aop:config> 
    <aop:advisor advice-ref="customizableTraceInterceptor" 
     pointcut="execution(* fr.my.package.dao..*.*(..))" /> 
    </aop:config> 

</beans> 

fr.my.package.dao 패키지의 모든 클래스의 모든 메소드의 각 호출에 추적을 얻을하는 데 도움이.

덕분에 @ m-deinum