so I had a look into best practices regarding logging in Java (slf4j, log4j, logback, etc.) and it seems, that there is a consent among libraries how a logging API should look like. The recommendation I read all the time is code like this, where you create a logger per class:
class Foo{
Logger logger = Logger.getLogger(Foo.class);
//...
logger.info("my log message");
//...
}
Now I don't really get the benefit of having seperate loggers for different classes. Why are the info/debug/etc. methods in the Logger class not static?
Please don't give an anwser like "The benefit is that you can configure your logger depending on the class. For example you could set the log level for individual classes to DEBUG...", because I think this is something that you could accomplish with a completely static Logger class as well.
If you look around (e.g. here on stackoverflow: Log4J: Strategies for creating Logger instances) you find people using idioms like this:
Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClass().getCanonicalName());
Now instead of creating a logger for every class, you could just call the above code inside of a static (e.g.) info() method, where you could determine the calling class and apply all the logger configuration equally as you would do otherwise.
So can anyone tell me a real reason why someone should use a logger per class instead of using one static logger? Is it for purely historical reasons? Am I missing something? I am aware that there might be a performance impact, but it will be actually positive in terms of memory usage (and of course negative in terms of runtime)...
UPDATE I figured you can even use this code in static info/debug/etc. methods, which is beautiful and more performant than above code:
Reflection.getCallerClass()
However support for that method seems problematic >=JDK7