I have some Java code that I'd like to instrument with log messages for debugging purposes. The final (compiled) production code, however, should not contain any logging as it would slow down the execution time. Is there any way in Java to disable a logger at compile time?
I am not afraid of the footprint a check inside the log method of a run-time enabled/disabled logger would add.
if (logging==enabled) {// do logging}
But I'd like to avoid parameter construction like the following in my production code:
Logger.log("undefined state" + state + " @ " + new Date());
I am using Sun's Java Compiler.
Have you considered the slf4j approach with {}-placeholders. That allows for delayed construction of the toString() meaning that log.debug(...) is cheap if debug logging is disabled.
log.debug("in loop() - a={}, b={}", a, b);
if(logger.isDebugEnabled()) {
logger.debug(expression);
}
Every logging framework I've used requires you to use the above pattern to avoid unnecessary evaluation of the logging expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With