Click here to Skip to main content
13,094,732 members (96,896 online)
Rate this:
 
Please Sign up or sign in to vote.
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 3-Sep-12 23:28pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2017
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