Skip to content

Instantly share code, notes, and snippets.

@slumbi
Last active August 29, 2015 14:07
Show Gist options
  • Save slumbi/cc4bf7e28073fbf5e60d to your computer and use it in GitHub Desktop.
Save slumbi/cc4bf7e28073fbf5e60d to your computer and use it in GitHub Desktop.
simple method logger AOP
package fr.aspects;
import org.apache.log4j.Logger;
@Aspect
public class MethodLogger {
Logger logger = Logger.getLogger(MethodLogger.class);
// @Around("execution(* *(..)) && @annotation(Loggable)")
@Around("execution(* *(..))")
public Object around(ProceedingJoinPoint point) {
long start = System.currentTimeMillis();
Object result = null;
try {
result = point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = System.currentTimeMillis() - start;
if (diff > 30)
logger.info(String.format("#%s(%s): %s in %s [msec]", MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
point.getArgs(), result, diff));
return result;
}
}
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
...
...
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>compile_with_aspectj</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>utf-8</encoding>
<complianceLevel>1.6</complianceLevel>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<!--
<sources>
<source>
<basedir>${basedir}/src/main/java</basedir>
<includes>
<include>**/*Aspect.java</include>
</includes>
</source>
</sources>
-->
</configuration>
</plugin>
</plugins>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment