Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log the time taken by methods in Springframework?

Is it possible in springframework to log the time taken by methods [ selective | all ] automatically. By automatically i mean, i don't want to go to each method and write the log.debug ( "...." ); stuff.

like image 690
Rakesh Juyal Avatar asked Sep 30 '09 05:09

Rakesh Juyal


2 Answers

AOP is what you need here. AOP allows you to add code to your application without modifying the original code. Spring AOP prefers to accomplish this with Proxy objects. Proxy objects use a Decorator Pattern to wrap the original Target object and add code. The Proxy is configured to implement one or more interfaces of the original Target object.

Here, to time an application, the idea is to use the PerformanceMonitorInterceptor, one of the performance monitoring classes that ship with the Spring Framework.

The first option is to use the Spring class ProxyFactoryBean to create Spring AOP Proxy objects. To do this:

  • Define your original bean:
  • Define a PerformanceMonitorInterceptor:
  • Define a RegexpMethodPointcutAdvisor:
  • Define a ProxyFactoryBean to proxy your original bean and apply your Advisor
  • Set the Log level for the PerformanceMonitorInterceptor to TRACE

Below a Spring configuration that illustrates these steps:

<beans>
  <bean id="MyServiceTarget" class="org.myapp.services.MyService">
    <property ... />
  </bean>

  <bean id="timingLogger" class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"/>

  <bean id="timingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="timingLogger"/>
    <property name="patterns">
      <list>
        <value>.*</value>
      </list>
    </property>
  </bean>

  <bean id="MyService" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>org.myapp.services.MyService</value>
    </property>
    <property name="target"><ref local="MyServiceTarget"/></property>
    <property name="interceptorNames">
      <list>
        <value>timingAdvisor</value>
      </list>
    </property>
  </bean>
</beans>

And the configuration of the Log level for the PerformanceMonitorInterceptor:

log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE

Starting with Spring 2.0, there is another option: using Spring 2.0 XML Schema-based configuration and Spring's AspectJ style pointcut expressions. With the ProxyFactoryBean you have to explicitly declare the interfaces you want to proxy; using the <aop:config> and <aop:advisor> tags, you can automatically proxy every interface of every object in the bean container.

<beans "add xsd declarations here" >
  <bean id="MyService" class="org.myapp.services.MyService">
    <property ... />
  </bean>

  <bean id="timingAdvice"
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"/>

  <aop:config>
    <aop:advisor pointcut="execution(* org.myapp.services.MyService.*(..))"
      advice-ref="timingAdvice"/>
  </aop:config>
</beans>
like image 70
Pascal Thivent Avatar answered Nov 03 '22 01:11

Pascal Thivent


You can take a look at stagemonitor. It is a open source java web application performance monitor. It captures response time metrics, JVM metrics, request details (including a call stack captured by the request profiler) and more. The overhead is very low.

Optionally, you can use the great timeseries database graphite with it to store a long history of datapoints that you can look at with fancy dashboards.

Example Screenshot: enter image description here

Take a look at the project website to see more screenshots, feature descriptions and documentation.

Note: I am the developer of stagemonitor

like image 42
Felix Avatar answered Nov 03 '22 02:11

Felix