Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need help in the Logging Implementing using Spring AOP concept .
I am using Spring mvc with rest webservice in my project where flow goes in from web.xml->rest-servlet.xml->controller->Dao->DataBase.

i have implemented by providing code with AspectLogging as LoggingAspect.java
Logging Aspect.java:
--------------------------

package com.apple.cmpd.util;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
Logger log = Logger.getLogger(getClass());
@Around("execution(com.*)")
public void logAround(JoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object clazz = pjp.getTarget().getClass().getName();
String methodName = pjp.getSignature().getName();
log.info("***************************************** Start **************************************");
log.info("Entering to Class " + clazz + " With Method Name " + methodName);
Object output = pjp.getTarget().getClass().getName();
log.info("Method Excecution Completed " + methodName + " in Calss " + clazz);
long elapsedTime = System.currentTimeMillis() - start;
log.info("Method execution time for method " + methodName + " in Calss " + clazz + ":" + elapsedTime + " milliseconds.");
log.info("***************************************** End **************************************");
}

@Before("execution(com.*)")
public void logBefore(JoinPoint joinpoint) {
Object clazz = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
log.info("Entering to Class " + clazz + " With Method Name " + methodName);
if (log.isDebugEnabled()) {
Object[] obj = joinpoint.getArgs();
for (Object o : obj)
log.info("Parameter Name..." + o != null ? o.toString() : "");

}
}
@After("execution(com.*)")
public void logAfter(JoinPoint joinpoint) {
Object clazz = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
log.info("After Entring to Method " + methodName+ " in Calss " + clazz);

}
@AfterReturning("execution(com.*)")
public void logAfterReturning(JoinPoint joinpoint) {
Object clazz = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
log.info("After Returning From Method " + methodName + " in Calss " + clazz);

}
@AfterThrowing(value = "execution(com.*)", throwing = "ex")
public void logAfterThrowing(JoinPoint joinpoint, Exception ex) {
Object clazz = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
log.info("After Throwing From Method " + methodName + " in Calss " + clazz);
log.error("After Throwing From Method " + methodName + " in Calss " + clazz, ex);
ex.printStackTrace();
}
}

where this above java code intitially loads in rest-servlet.xml which inturn called by web.xml.

rest-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" >
<context:annotation-config xmlns:context="#unknown">

<context:annotation-config xmlns:context="#unknown">
<context:component-scan base-package="com">
<aop:aspectj-autoproxy xmlns:aop="#unknown">


<!-- Aspect Mapping -->
<bean id="logAspect" class="com.LoggingAspect">

<aop:config xmlns:aop="#unknown">
<aop:aspect id="aspectLoggging" ref="logAspect">
<!-- Pointct Mapping -->
<aop:pointcut id="pointCutBefore">
expression="execution(* com(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore">
<!-- @After -->
<aop:pointcut id="pointCutAfter">
expression="execution(* com(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter">
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning">
expression="execution(* com(..))" />
<aop:after-returning method="logAfterReturning">
returning="result" pointcut-ref="pointCutAfterReturning" />
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing">
expression="execution(* com(..))" />
<aop:after-throwing method="logAfterThrowing">
throwing="error" pointcut-ref="pointCutAfterThrowing" />
<!-- @Around -->
<aop:pointcut id="pointCutAround">
expression="execution(* com(..))" />
<aop:around method="logAround" pointcut-ref="pointCutAround">




web.xml:-
------------

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application
<context-param>
<param-name>log4jRefreshInterval
<param-value>1000

<listener> <listener-class>org.springframework.web.util.Log4jConfigListener <servlet>
<servlet-name>rest
<servlet-class>
org.springframework.web.servlet.DispatcherServlet

<init-param>
<param-name>config-api
<param-value>WEB-INF/rest-servlet.xml

<load-on-startup>1

<servlet-mapping>
<servlet-name>rest
<url-pattern>/rest/*



Can any one Suggest me based on the above code given is it the correct approach for implementing the Logging mechanism to plugin to my project or if not please share me if there was any end to end working code of spring aop logging code in the realtime.
Posted
Updated 11-Sep-12 21:02pm
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900