Back Forum Reply New

Logging using AOP interceptor

Hello All,

In my web application, i have to log start and end of each method along with the time it took to execute the method. So I created following LogInterceptor class.

public class TestLogInterceptor implements MethodInterceptor,
MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{
private static final Log logger = LogFactory.getLog(TestLogInterceptor.class);
public Object invoke(MethodInvocation methodInvocation) throws Throwable {   //logger.info(quot;BEGIN: quot; + methodInvocation.getMethod().getDeclaringClass() + quot;::quot; + methodInvocation.getMethod().getName());      long startTime = System.currentTimeMillis();   try   {     Object retVal = methodInvocation.proceed();     return retVal;   }   finally   {     //logger.info(quot;END: quot;  + methodInvocation.getMethod().getDeclaringClass() + quot;::quot; + methodInvocation.getMethod().getName());     //logger.info(quot;Method invocation time: quot; + (System.currentTimeMillis() - startTime) + quot; msecs.quot;);             } }  public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { logger.info(quot;BEGIN: quot;+arg0.getDeclaringClass()+quot; quot;+arg0.getName()+quot; quot;+System.currentTimeMillis());
}

public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

logger.info(quot;END: quot;+arg1.getDeclaringClass()+quot; quot;+arg1.getName()+quot; quot;+System.currentTimeMillis());
}

public void afterThrowing(Method m, Object[] args, Object target, Throwable ex)
{
logger.info(quot;EXCEPTION: quot;+ex.getStackTrace().toString()+quot; quot;+m.getDeclaringClass()+quot; quot;+m.getName()+quot; quot;+System.currentTimeMillis());
}

}and the I configure it in the applicationContext.xml as follows

lt;bean name=quot;logAutoProxyquot; class=quot;org..aop.framework.autoproxy  .BeanNameAutoProxyCreatorquot;gt;  lt;property name=quot;beanNamesquot;gt;
lt;valuegt;com.myapp.test.*lt;/valuegt;  lt;/propertygt;  lt;property name=quot;interceptorNamesquot;gt;    lt;listgt;        lt;valuegt;logInterceptorlt;/valuegt;    lt;/listgt;  lt;/propertygt;
lt;/beangt;

lt;bean id=quot;logInterceptorquot; class=quot;com.jpmchase.srgt.interceptor.TestLogInterc  eptorquot; /gt;

But i am not sure why calls are not getting intercepted by the loginterceptor.

The same thing works when I test my classes using JUnit test case as follows.

ApplicationContext ctx =
new FileSystemXmlApplicationContext(quot;C:/DEV/myapp/WebRoot/WEB-INF/applicationContext.xmlquot;);

test=(TestService)ctx.getBean(quot;testBeanquot;);Could you tell me why interceptor is not working in the web application?

Thanks,
Shriram

The beanNames property is suppose to be names of beans in the application context, not names of classes.

Can I configure my LogInterceptor in such way that it intercepts calls to all the classes?

You may use the auto-proxy feature to automatically apply your interceptors to appropriate beans. See sp...#aop-autoproxy for more details.

-Ramnivas

Note whenever using System.currentTimeMillis(), JAMon provides a more flexible solution by using the Spring jamon interceptor.

docs/...terceptor.html

You can look at sql proxy capability.

A good forum posting that discusses the jamoninterceptor among other things.

articles/Simpli...nterprise-Apps

A good forum posting that discusses the jamoninterceptor among other things.

articles/Simpli...nterprise-Apps

Hi,

I tried autoproxy feature. But the issue is this works with standalone java program but not with my web application.

in standalone program i load the bean as follows and it works

ApplicationContext ctx = new FileSystemXmlApplicationContext(quot;C:/DEV/myapp/WebRoot/WEB-INF/applicationContext.xmlquot;);

what is the issue? Please help

- Shriram.

gurus...please reply....

I checked log files.......I see following message in it...

[INFO] XmlWebApplicationContext - Bean 'logAutoProxy' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)What does this mean? How can I resolve the issue?

- Shriram
¥
Back Forum Reply New