I've been working with a codebase of a company that has a policy of writing lots of trace logging. So pretty much every method has a piece of code that starts like this:
String LOG_METHOD = "nameOfMethod(String,List<Long>):void";
if(logger.isTraceEnabled()) {
Object[] params = new Object[] { string, list };
logger.trace(CompanyMessages.newMethodInstanceMessage(this, LOG_METHOD, params));
}
and end like this (either in a finally
-clause or just at the end of the method:
if(logger.isTraceEnabled()) {
logger.trace(CompanyMessages.leaveMethodInstanceMessage(this, LOG_METHOD));
}
There is actually more code to it, but this is the basic idea.
This is cluttering the code and other coders are constantly messing it up with their own interpretations which don't use the specific CompanyMessages
-class which is needed to format the messages to be read by the monitoring tools. So I am looking for a way to get rid of all code above and just provide all methods which need trace-logging with annotations like: @LogBefore('logLevel')
& @LogAfter('logLevel')
.
The reason I choose this solution is to make it so other developers don't have to learn anything new but to use annotations instead of code. I'm working in a server environment in which we deploy hundreds of web applications and dozens of developers. So I have been looking for a way to implement this in a web application without a lot of extra coding or additional large libraries. This means I'm looking for a small, stable AOP implementation using annotations similar to those I proposed, easy to configure in each web application. Performance is also important. What is the simplest example to implement this with AOP?
Edit: I did find something very similar to what I'm looking for, but this has a couple of problems. All classes that need logging must be configured, which would be more resource intensive than just using annotations. Would the spring configuration <aop:aspectj-autoproxy/>
fix that?