Created
August 4, 2011 12:38
-
-
Save jamesmorgan/1125076 to your computer and use it in GitHub Desktop.
Sample Logging Aspect
This file contains hidden or 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
@Aspect | |
public class LoggingAspect { | |
private ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory(); | |
@Before("execution(@com.morgan.design.demo.annotation.LogMe(com.morgan.design.demo.annotation.LoggingLevel.WARN) * *(..))") | |
public void loggerWarn(final JoinPoint jp) throws Throwable { | |
final Logger logger = getLoggerForClass(jp); | |
logger.warn(buildLogging(jp)); | |
} | |
private Logger getLoggerForClass(final JoinPoint jp) { | |
return this.iLoggerFactory.getLogger(jp.getSignature() | |
.getDeclaringType() | |
.getName()); | |
} | |
private String buildLogging(final JoinPoint jp) { | |
final StringBuilder message = new StringBuilder(); | |
message.append("Method Invocation=[") | |
.append(jp.getSignature() | |
.getName()) | |
.append("]") | |
.append(" - Args=") | |
.append(Arrays.toString(jp.getArgs())); | |
return message.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment