I am trying to track down an issue in one of my Java projects and I am currently facing the possibility of having to manually add a lot of logging statements to a whole bunch of methods. As you can probably understand, I am not very fond of that idea:
-
Most of those methods are performance-critical and I’d rather not add logging when it’s not needed, even if it’s something as lightweight as log4j.
-
In some cases I’d like to log the operation of classes from the Java library. While the OpenJDK source code is available, I am not going to provide my own Java library version just for this.
-
I am interested in logging specific code paths under specific conditions. While I could probably centralize the logging applicability checks, I still foresee unforeseen refactoring in my future, should I need to modify the conditions.
-
Adding logging statements en-mass requires a lot of work and time, which I’d rather use more productively.
Considering the existence of bytecode manipulation libraries, such as Javassist and BCEL, I was hoping that there might be a logging framework that would take the grunt work out of my hands.
Since I am mainly interested in logging method call arguments and return values under specific conditions, is there an instrumentation-based logging framework for Java that would allow me to do this? Preferably something somewhat more modern and a little less awkward than Java agents?
Logging is the “hello world” of aspect-oriented programming.
If you’re already a Spring user, you’ll be able to write a single aspect class to log what you want and then weave it declaratively into all the objects that need it.
Spring has its own AOP implementation; it also supports AspectJ.
You can use AspectJ without Spring if that’s your preference.