Skip to content


So everybody knows printing debugging statements to the log file can take considerable amounts of time, right? And in order to decrease useless time logging debug statements that won’t appear in the log, you can use isDebugEnabled() to find out if you’re wasting your time, right? So you end up with;

if (log.isDebugEnabled()) {
    log.debug("hello world");

1000 times in your code – everywhere you want a debug statement.

But let’s say you don’t want to rely directly on logging framework. So in your infinite wisdom you write a logging Decorator, which decorates the Log4J Logger class.

I mean, now we can rid of all those 1000 if statements, right, and replace it with just one.

public class MySpecialLogger {
    private Logger log = Logger.logger(MySpecialLogger.class.getName());
    public void debug(String message) {
        if (log.isDebugEnabled()) {

But no – not if you are scared to refactor your code. You might end up with;

if (mylog.isDebugEnabled()) {
    mylog.debug("hello world");
// do absolutely nothing else
if (mylog.isDebugEnabled()) {
    mylog.debug("goodbye cruel world");

Hundreds, if not thousands, of times in your code. And some other programmer can clean it up.


  1. Calvin Yu wrote:

    I think most logger implementations has that check internally, so having that if statement is overkill for static strings. if(log.isDebugEnabled()) is useful in cases where you’re spending time building the message to log, like if you’re doing a lot of string concats. Some logging frameworks offer a MessageFormat like way optimizing this if block out, so you can do something like this:

    mylog.debug(“Hello, {0}”, username);

    Wednesday, January 14, 2009 at 22:54 | Permalink
  2. scot wrote:

    Oh yes, absolutely Calvin. In fact I think that even poor old Log4J has the ‘isDebugEnabled’ built-in, “under the hood”. I should point out in nearly every single case where something was being logged, it was a case of log.debug(“i’m in ur loop”). With no string concats.

    I would question, though, whether it’s even really necessary for a simple two-string concatenation, for example.

    Even for a custom Decorator class, there might be simpler way to implement it, such as a varargs “debug(String … )” method, that does the check then concatenates all the passed in strings.

    My point was really about environments where “refactor fearlessly” has completely disappeared out the window, causing extreme timdity on the part of developers to change almost any code, even over something as trivial as logging.

    Wednesday, January 14, 2009 at 23:12 | Permalink