Announcement Announcement Module
Collapse
No announcement yet.
Log4j refernace in afterReturning Method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Log4j refernace in afterReturning Method

    I have been playing around with AOP and lately as a way to help me with logging. I am able to get all the AOP stuff working and I am able to log with Log4j. Bu

    Code:
    Logger  logger; 
        public void afterReturning(Object object, 
                                 Method m, 
                                 Object[] args, 
                                 Object target) 
                                 throws Throwable {
            //Get the logger from the class who's method invoke this aop method
    
            logger.debug("The method (" + this.getClass().getName() + ") of " + target.getClass().getName() + " has just been exited.");
        }
    Rather than doing the below in the above method

    Code:
    Logger  logger = Logger.getLogger(target.class);
    I would like to get the logger from the class who invoked my aop method afterReturning. I hope this makes sense.

    PS: Really just looking for the best way to log using spring and aop. I feel as though I may be missing some mystical bigger picture.

  • #2
    Spring AOP is based on Java Dynamic Proxies, so without knowing much about the rest of the structure of your code I would say your afterReturning(..) advice is invoked by some type of InvocationHandler.
    So the real question is: Are you using Spring AOP or AspectJ. The reason why I am asking is because with Spring AOP you do not have access to a caller context (call(..) pointcut), thus you do not have access the state of the object that is calling your target, whereas using AspctJ you would and therefore would be able to access via Reflection all instance as well as static variables of the class that is calling your target while being intercepted (before, after or around) by your afterReturning(. .) method.
    It's not difficult and I'll be glad to share some code, but you have to give more info as to what are you trying to achieve.

    Comment


    • #3
      AOP Log4j

      I am using spring AOP

      Comment


      • #4
        Could you elaborate a little more on what r u trying to do?
        Otherwise the answer is just as short: You do not have access the caller context using Spring AOP...

        Comment


        • #5
          "caller context using Spring AOP" Is there a place where I can read about this. not sure what it is.

          Below is my factory bean, advice bean and my target bean. The advice is getting executed after a method on my target bean is executed.

          I just wanted to know

          Code:
          	<!-- Bean configuration -->
             <bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="proxyInterfaces">
                   <value>com.IBusinessLogic</value>
                </property>
                <property name="target">
                   <ref local="beanTarget"/>
                </property>
                <property name="interceptorNames">
                   <list>
                      <value>theTracingAfterAdvisor</value>
                   </list>
                </property>
             </bean>
          Code:
             <!-- Advisor pointcut definition for after advice -->
             <bean id="theTracingAfterAdvisor"
                class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                <property name="advice">
                   <ref local="theTracingAfterAdvice"/>
                </property>
                <property name="pattern">
                   <value>.*</value>
                </property>
             </bean>
          Code:
             <!-- Bean Classes -->
             <bean id="beanTarget" class="com.BusinessLogic"/>

          In the below code

          Code:
          import java.lang.reflect.Method;
          
          import org.apache.log4j.Logger;
          import org.springframework.aop.AfterReturningAdvice;
          
          public class TracingAfterAdvice implements AfterReturningAdvice {
          	
          	Logger  logger; 
              public void afterReturning(Object object, 
                                       Method m, 
                                       Object[] args, 
                                       Object target) 
                                       throws Throwable {
              	
              	logger = Logger.getLogger(target.getClass());
                  logger.debug("---------------he method (" + this.getClass().getName() + ") has just been exited.");
              }
          }
          is

          Code:
            
          logger = Logger.getLogger(target.getClass());
          causing a performance bottle neck?

          Comment


          • #6
            Not any more then if you were to insert the log statement inside of the business code. . . may be some nano-second on proxy creation.
            AOP addresses modularization to simplify development and design efforts. Cross-cutting concerns such as logging and tracing could now be isolated in the single module and applied everywhere based on the pointcut expression. Without AOP you would have to manually insert the code that implements cross-cutting concern everywhere mixing it with business logic.

            Comment

            Working...
            X