Is a logger per class or is a set of loggers that are accessed by the entire application perferred?

I use one logger per class for all technical aspects, e.g. logging, tracing, debugging, method in/out (private static final...). Often I used shared loggers for functional aspects, e.g. audit trails, security; public Logger in a shared class.

I'm not aware of standards or general best practices, but this approach worked well for me.


A logger in each class is better and more easy to extend. The reason is that defining one logger in one class easily separates the real logging api from the logger's configuration (format, persistence). I have worked with more than one big and complex java software (> 1 million lines of code), they all use one logger per class.

Also, definition of "one logger per class" doesn't mean that each class uses a different logger instance.

class Foo {
    private static final Logger log = Logger.getLogger( Foo.class );
}


class Bar {
    private static final Logger log = Logger.getLogger( Bar.class );
}

Whether the two loggers referenced in the above code use the same logger instance is not clear. Normally, there is a configuration place (in a property file or in the code) that specifies whether the loggers in Foo and Bar share a logger instance.

So "a logger in each class" just defines one logger for every class, but such loggers may refer to the same logger instance used in different classes

Tags:

Java

Oop

Logging