Monday, April 23, 2007

Using Java instrumentation to remove logging

Bytecode instrumentation is a common technique used to mainpulate an existing compiled class at either compile time or runtime. I'm used to seeing instrumentation used to add functionality to a class - e.g. add event logging, add statistics counts to method calls, add persistence capability to a POJO - but I came across someone who has used instrumentation to remove functionality.

The idea? Remove a functio call to log4j at runtime if that call is below the default logging level. This way the performance penalty to even the isDebugEnabled()/isInfoEnabled()/etc calls are completely removed. This is really, really clever.

The only downside to this approach that I can think of is if you dynamically change your application log levels downwards then you won't see any additional logging because the logging calls have already been removed.

No comments: