Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: J2EE
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:
 

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" >

 



 

<!-- Aspect Mapping -->




<!-- Pointct Mapping -->

expression="execution(* com(..))" />

<!-- @After -->

expression="execution(* com(..))" />

<!-- @AfterReturning -->

expression="execution(* com(..))" />

returning="result" pointcut-ref="pointCutAfterReturning" />
<!-- @AfterThrowing -->

expression="execution(* com(..))" />

throwing="error" pointcut-ref="pointCutAfterThrowing" />
<!-- @Around -->

expression="execution(* com(..))" />



 

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" >

Archetype Created Web Application

log4jRefreshInterval
1000


org.springframework.web.util.Log4jConfigListener


rest

org.springframework.web.servlet.DispatcherServlet


config-api
WEB-INF/rest-servlet.xml

1


rest
/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 4-Sep-12 0:28am
Edited 11-Sep-12 22:02pm
v6

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

  Print Answers RSS
0 OriginalGriff 319
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 230
3 CPallini 205
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100