An easy way to enable tracing of methods and parameter values in Java Spring is by using AspectJ. A simplified example of my Aspect class to make tracing easy, without any ‘logger.trace’ code in the classes itself:
package com.markvandenbergh.test.util;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class LoggerAspect {
private final Logger logger = LoggerFactory.getLogger(LoggerAspect.class);
// before the execution of any method with any number and type of parameters
// in package 'test' and any sub-package execute the following code...
@Before("execution(* com.markvandenbergh.test..*.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
if(logger.isTraceEnabled()) {
// constructing a string of the method signature, method and parameters
String traceString = joinPoint.getSignature().getDeclaringTypeName() +
"." + joinPoint.getSignature().getName() + "(";
Object[] args = joinPoint.getArgs();
for(int i = 0; i < args.length; i++) {
if(args[i] != null) {
traceString += args[i];
} else {
traceString += "null";
}
if(i < (args.length -1)) {
traceString += ", ";
}
}
traceString += ")";
logger.trace(traceString);
}
}
}
If tracing is enabled this will log the entry of any method with it’s parameters within the com.markvandenbergh.test package (sub-packages included, because of the double dot between ‘test’ and ‘*.*’). Example of the output:
2012-01-26 15:51:55,942 TRACE http-bio-8080-exec-4 com.markvandenbergh.test.service.TestService.methodNumberOne(parameterValue)
2012-01-26 15:51:55,942 TRACE http-bio-8080-exec-4 com.markvandenbergh.test.service.TestRepository.methodInRepository(parameterValue)
Do note that only Spring beans will be picked up by the Aspect. If you have regular Java classes that are not instantiated using Spring they will not be logged using this code.
You also need some minor Spring configuration for this.
<aop:aspectj-autoproxy /> <bean class="com.markvandenbergh.test.util.LoggerAspect" />
The Maven dependencies for this are:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
All of this is nothing new or fancy, but I found it quite useful in reducing the amount of work and boilerplate code in my classes. Some other useful ‘advice’ types (that’s what the ‘before’ is called) are: ‘after’ and ‘around’. ‘After’ works about the same as the ‘before’ in my example, for an example of the ‘around’ advice go to the Spring framework reference documentation.
