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?
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.
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