Announcement Announcement Module
Collapse
No announcement yet.
Use of logging interceptor in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use of logging interceptor in Spring

    Hi Experts and Guiders,

    I wish to implement the concept of AOP - logging.

    Could anyone suggest me the url or steps to be followed to have the logging aspect in my web application(struts+spring).

    I have googled and followed steps , but it stopped in the middle.
    I have created a class as below:
    import java.lang.reflect.Method;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.MethodBeforeAdvice;
    import org.springframework.aop.ThrowsAdvice;

    public class LoggingInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{
    private static Log log = null;

    public LoggingInterceptor(){
    }

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

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

    public void afterThrowing(Method m, Object[] args, Object target, Throwable ex)
    {
    log = LogFactory.getLog(target.getClass());
    log.info("Exception in method: "+m.getName()+" Exception is: "+ex.getMessage());
    }

    }

    *****************
    Next i have made entry in applicationContext.xml file as follows:

    <bean id="updateMyDetailsService" class="org.xxx.yyy.service.UpdateMyDetailsServiceI mpl">
    <property name="updateMyDetailsDAO">
    <ref bean="updateMyDetailsDAO" />
    </property>
    </bean>
    <bean id="updateMyDetailsServiceBean"
    class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>org.xxx.yyy.service.UpdateMyDetailsServic e</value>
    </property>
    <property name="target">
    <ref bean="updateMyDetailsService"/>
    </property>
    <property name="interceptorNames">
    <list>
    <value>loggingInterceptor</value>
    </list>
    </property>
    </bean>
    <bean id="loggingInterceptor" class="org.xxx.yyy.util.LoggingInterceptor"/>

    ***************
    I dont know, what is the next step.

    Can anyone provide a new solution(or link ) or next steps to the above solution.

    Thanks

  • #2
    Simple steps for logging using Spring

    Step 1:
    In applicationContext.xml file:

    <bean id="loggingInterceptor" class="xxx.yyy.LoggingInterceptor"/>

    <bean id="updateService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="target" >
    <bean class="xxx.yyy.UpdateServiceImpl">
    <property name="updateDAO">
    <ref bean="updateDAO" />
    </property>
    </bean>
    </property>
    <property name="interceptorNames">
    <list><value>loggingInterceptor</value></list>
    </property>
    </bean>

    Step 2:
    create the class LoggingInterceptor.java in xxx.yyy folder

    import java.lang.reflect.Method;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.MethodBeforeAdvice;
    import org.springframework.aop.ThrowsAdvice;

    public class LoggingInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{
    private static Log log = null;

    public LoggingInterceptor(){
    }

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

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

    public void afterThrowing(Method m, Object[] args, Object target, Throwable ex)
    {
    log = LogFactory.getLog(target.getClass());
    log.info("Exception in method: "+m.getName()+" Exception is: "+ex.getMessage());
    }

    }


    >>>>>>>>>>>

    By the above solution, we can see the entry ,exit, exception for each method in the xxx.yyy.UpdateServiceImpl

    Note:UpdateServiceImpl is implementation for Interface UpdateService .

    By writing the property "updateDAO", we can have mapping from service to DAO layer.

    Comment


    • #3
      Hmmm...use Spring 3 AOP with annotation...it's far more powerful and far less painful...

      Comment


      • #4
        Hi Enrico ,
        Thanks for the update. I wish to learn to implement the AOP with Annotation.
        Could you please provide the sample code and steps to be taken to implement the same concept using AOP with Annotations.

        Comment


        • #5
          This is how I implemented a logging aspect for one of my web applications:

          - Aspect class:
          Code:
          public class LoggingAspect
          {
          	public Object log(ProceedingJoinPoint call) throws Throwable 
          	{
          		Signature sign = call.getSignature();
          		Logger log = Logger.getLogger(sign.getDeclaringTypeName());
          		log.debug("Beginning method " + sign.getName());
          		log.debug("Method signature: " + sign.toLongString());
          		Object[] args = call.getArgs();
          		if (args != null && args.length != 0)
          		{
          			log.debug("Input parameters:");			
          			for (int i=1; i<=args.length; i++)
          			{
          				if (args[i-1] != null)
          					log.debug("---> " + i + ": type " + (args[i-1].getClass() != null ? 
          						args[i-1].getClass().getName() : "undefined") + " - value: " + args[i-1].toString());
          				else
          					log.debug("---> " + i + ": null");
          			}
          		}
          		Object result = call.proceed();
          		log.debug("Ending method " + sign.getName());
          		if (result != null)
          			log.debug("Method returned value: " + result.toString() + " of type " + 
          				result.getClass().toString());
          		return result;
          	}
          }
          - xml configuration:
          Code:
            <aop:config proxy-target-class="true">
              <aop:aspect ref="loggingAspect">
                <aop:pointcut id="loggingPointcut" expression="(within(gesic.dao..*) || within(gesic.services..*)) and execution(* *(..))"/>
                <aop:around method="log" pointcut-ref="loggingPointcut"/>
              </aop:aspect>
            </aop:config>
            
            <bean id="loggingAspect" class="gesic.aspects.LoggingAspect"/>

          Comment


          • #6
            Thank you..Expert

            Thanks a lot for a simple and feasible solution.

            Comment


            • #7
              exception handling

              Hi All,

              Can anyone please provide the example for exceptionhandling using Spring.

              Simple example

              Comment

              Working...
              X