23

I've found a lot of examples on how to create a custom aspect for logging using the Spring framework like this or this but did not find standard/common Spring implementation for this situation and question. Are there any standard implementations of logging aspect from Spring or not?

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45

2 Answers2

24

Yes there are!

<bean id="customizableTraceInterceptor" class="org.springframework.aop.interceptor.CustomizableTraceInterceptor">
    <property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
    <property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
    <aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * BankAccountServlet.*(..))"/>
</aop:config>

Check out the CustomizableTraceInterceptor API, you can define separate enter/exit/exception messages with several placeholders:

  • $[methodName] - replaced with the name of the method being invoked
  • $[targetClassName] - replaced with the name of the class that is the target of the invocation
  • $[targetClassShortName] - replaced with the short name of the class that is the target of the invocation
  • $[returnValue] - replaced with the value returned by the invocation
  • $[argumentTypes] - replaced with a comma-separated list of the short class names of the method arguments
  • $[arguments] - replaced with a comma-separated list of the String representation of the method arguments
  • $[exception] - replaced with the String representation of any Throwable raised during the invocation
  • $[invocationTime] - replaced with the time, in milliseconds, taken by the method invocation
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • It is interesting, tomorrow will try it, Could you publish link where can I read about it? Thanks – Sergii Zagriichuk Sep 04 '11 at 21:19
  • @Tomasz - i tried this in Eclipse Virgo server from a OSGI bundle. it didn't print any log to the log file. i doubt its because of the weaving issue. but i dnt know exactly. can you please help me? Thanks in advance. – Sam Mar 24 '12 at 05:46
  • @Sam: I am not very exprienced with OSGi, but I know this code snippet works in standard environment, sorry. Have you tried running it in a standalone application (single bundle running in JUnit test?) – Tomasz Nurkiewicz Mar 28 '12 at 09:30
  • @TomaszNurkiewicz - yes of course.this works fine in normal environments.i was still unable to figure out whats happening when it comes to OSGI..Thanks for your response tomasz.. – Sam Mar 28 '12 at 17:04
1

Here are the list of frameworks that do logging via AOP:

http://aspect4log.sf.net - does very nice looking logging via slf4j and @Log annotation. Can work via SpringAOP, and AspectJ. With AspectJ it works even for private methods and constructors and does not require a class to be a Spring Bean. Very easy to use, i was able to make it running with my project within 5 min.

http://loggifier.unkrig.de - does logging via java.util.logging, a bit too complex and not that well document but claims that it can instrument already compiled jar/war/ear files!

AbstractTraceInterceptor (from SpringAOP) and it's subclasses SimpleTraceInterceptor and CustomizableTraceInterceptor. Logging configuration is done separately from classes. Logs via commons-logging. Since it is designed for SpringAOP you have to work with Spring Beans (and only with Spring Beans public methods).

Mark D
  • 41
  • 2