Results 1 to 4 of 4

Thread: Spring AOP loggers append Adviser Class name instead actual class name

  1. #1

    Default Spring AOP loggers append Adviser Class name instead actual class name

    Followings are my proxy configuration, log4j.xml and Advice class for AOP loggers. I have separate logging methods in the Adviser class for method before, afterReturning as well as afterThrowing. The issue is, my log file shows adviser name always. Instead of this, i need to show actual class name. LogginInterceptor.java is my logging class.

    Sample log record -

    2012-05-18 17:48:24,173 [main] INFO (LogginInterceptor.java:20) - com.dialog.ezcash.recycle.number.service.NumberRec yclableImpl - Ending method: sendNotification


    ============================= Application-Context.xml ====================================



    <bean id="loggingIntercepter" class="com.dialog.test.recycle.number.advicer.Logg inInterceptor"/>

    <bean id="advicer" class="org.springframework.aop.support.NameMatchMe thodPointcutAdvisor">
    <property name="advice" ref="loggingIntercepter"></property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>*Service</value>
    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>loggingIntercepter</value>
    </list>
    </property>
    </bean>




    ============================= log4j.xml ====================================


    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %c - %m%n" />
    </layout>
    </appender>

    <appender name="LogDebug" class="biz.minaret.log4j.DatedFileAppender">
    <param name="Prefix" value="ezcash_number_recycle_" />
    <param name="Suffix" value=".log" />
    <param name="Directory" value="logs" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p (%13F:%L) %3x - %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="INFO" />
    <param name="LevelMax" value="INFO" />
    </filter>
    </appender>
    <appender name="LogError" class="biz.minaret.log4j.DatedFileAppender">
    <param name="Prefix" value="ezcash_number_recycle_" />
    <param name="Suffix" value=".log" />
    <param name="Directory" value="logs" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p (%13F:%L) %3x - %m%n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="ERROR" />
    </filter>
    </appender>

    <root>
    <appender-ref ref="LogDebug" />
    <appender-ref ref="LogError" />

    </root>
    </log4j:configuration>




    ============================= LogginInterceptor.java ====================================



    public class LogginInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{

    private static Logger log = Logger.getLogger(LogginInterceptor.class);

    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    log.info(arg2.getClass().getName()+" - Beginning method: "+arg0.getName());

    }

    public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
    log.info(arg3.getClass().getName()+" - Ending method: "+arg1.getName());
    }

    public void afterThrowing(Method m, Object[] args, Object target, Throwable ex) {
    log.error(m.getName()+" - Exception is: "+ex.getMessage());
    }
    }

  2. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    285

    Default

    This happens due to you have created the logger for the LogginInterceptor and not for the actual class. I guess you'll have to dynamically create a the logger for different invocations (this can be a overhead). On a different note, have a look at simpletraceinterceptor, Debuginterceptor, CustomizableTraceInterceptor too.
    Amila Domingo

  3. #3

    Default

    Thanks.... After longtime.. I am having similar problem as i am using custom error class extended by Exception. In AOP logging, i am using this custom adviser and i unable to read the custom message that set in my custom error class. It properly prints the stack trace.

    @AfterThrowing(pointcut="execution(* lk.dialog.xxx.xxx.xxx..*.*(..))",throwing= "error")
    public void afterThrowing(JoinPoint joinPoint, Throwable error) {
    log.info("######################################## #############");
    log.info("Exception in Method : " + joinPoint.getSignature().getName());
    log.info("Custome Error Message : " + error.getLocalizedMessage());
    log.error("StackTrace : ", error );
    log.info("######################################## #############");
    }


    Following is my exception class

    public class CEBUtilityPaymentException extends Exception{

    private static final long serialVersionUID = 5348958610545077524L;
    private String message;

    public CEBUtilityPaymentException(String message){
    this.message=message;
    }

    }



    This is the way i throw Exceptions

    throw new CEBUtilityPaymentException("ProductId is NULL or Empty");


    Logger Result

    2012-10-16 15:05:16,240 [main] INFO (LoggingIntercepter.java:36) - ################################################## ###
    2012-10-16 15:05:16,241 [main] INFO (LoggingIntercepter.java:37) - Exception in Method : generateFile
    2012-10-16 15:05:16,241 [main] INFO (LoggingIntercepter.java:38) - Custome Error Message : null
    2012-10-16 15:05:16,242 [main] ERROR (LoggingIntercepter.java:39) - StackTrace :
    lk.dialog.ezcash.utilityreport.ceb.exception.CEBUt ilityPaymentException
    at lk.dialog.ezcash.utilityreport.ceb.manager.CEBUtil ityManagerImpl.generateFile(CEBUtilityManagerImpl. java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:319)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:183)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :150)
    at org.springframework.aop.aspectj.AspectJAfterAdvice .invoke(AspectJAfterAdvice.java:42)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.aspectj.AspectJAfterThrowi ngAdvice.invoke(AspectJAfterThrowingAdvice.java:55 )
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.framework.adapter.MethodBe foreAdviceInterceptor.invoke(MethodBeforeAdviceInt erceptor.java:50)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:90)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy14.generateFile(Unknown Source)
    at lk.dialog.ezcash.utilityreport.ceb.run.Application Run.main(ApplicationRun.java:15)
    2012-10-16 15:05:16,248 [main] INFO (LoggingIntercepter.java:40) - ################################################## ###


    Thanks
    saminda

  4. #4
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    285

    Default

    Hello Saminda,

    Either you need to extend the getLocalizedMessage() and return the message property you have introduced or you need to pass the message value to the super constructor without saving it in your class.

    Take care
    Amila Domingo

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •