The Java docs for Logger indicate that the logger name should be based on the class name. Google Guice handles this in BinderImpl.java where it does the following:
return member == null
? Logger.getAnonymousLogger()
: Logger.getLogger(member.getDeclaringClass().getName());
However, since it’s getting a new logger for each class, I would lose access to whatever Handler’s may have been added to my logger.
What’s the best way to handle both having the class name used within the logger and having a standard set of handlers applied?
The solution actually has nothing to do with Guice, but rather, is based on how the logging classes work in Java.
Loggers are created in a hierarchy, thus, logger
x.y.zis a child ofx.y, all the way up the chain to the root logger whose name is the empty string. Each logger inherits properties, like its logging level, from its parent.Thus, in order to set the log level globally (or to set handlers globally), simply set them on the root logger:
Once that’s done, any injected loggers will have their properties derived from the root logger and any parent loggers that may exist. For more information on the basics, see the LogManager documentation.
A
configproperty also exists. Per the LogManager documentation:This implies that there are multiple solutions to the problem as I could have also used a configuration class.