Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to define a static application-wide logger?

Tags:

java

logging

I have a number of packages in my Java web application. In some of them I use this construct:

public class Foo {
  private static Logger logger = LoggerFactory.getLogger("com.XXX");
  public void bar() {
    Foo.logger.debug("Method bar() was just called");
  }
}

Of course, it's just an example. Everything works fine, but I don't like the idea of logger initialization in many classes. It's an obvious code duplication. I would like to move it to some helper class and let all other classes access it. Is it a correct idea? Or maybe there is some other "best practice" or a pattern?

like image 984
yegor256 Avatar asked Nov 05 '10 13:11

yegor256


1 Answers

Saying this is code duplication is like saying that having import java.util.* in 75% of your classes is code duplication. Or that you find yourself writing public class ... too often. It's not duplicating logic. It's duplicating boilerplate code.

Code duplication is only a design smell when it involves duplicating logic. If there is too much boilerplate code, that's a language design smell really. Nothing you should really be worried about.

On the downside, you lose the ability to fine-tune your logging if you abandon the hierarchal structure.

like image 171
Mark Peters Avatar answered Sep 25 '22 06:09

Mark Peters