Last active
August 29, 2015 14:07
-
-
Save slumbi/cc4bf7e28073fbf5e60d to your computer and use it in GitHub Desktop.
simple method logger AOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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