Re: Question about loggers
On 3/7/2012 4:52 PM, Novice wrote:
I'm working my way through Stephen Stelting's book Robust Java and have
some questions about the use of the Logging API (java.util.logging) in
Chapter 5.
My impression from the "Aspect Questions" thread was that the "best
practices" approach to logging was to use named loggers, where, in essence,
each method that needs to write to the log creates a named logger via
Logger logger = Logger.getLogger(getClass().getName());
and then writes to the log.
Did I get that right? Stelting mentions anonymous loggers and the global
logger starting on page 71.I see from the API (and by actually trying it in
the sample program below) that the global logger is no longer a recommended
technique but are there any common circumstances where a professional
program would use anonymous loggers? Or are they more a case of something
that might be used in a "sandbox" type program to try something out?
I can not see why saving to type something that give the class name
should be smart even for small things.
Also, Stelting mentions that you can use the LogManager to "cache Logger
objects for repeat use". What circumstances would justify using LogManager?
I was under the impression that simply doing Logger.getLogger() was
perfectly adequate for a professional quality program. Why would LogManager
be any better than simply making the logger a class variable?
Loggers are cached by default.
I have only used LogManager when I wanted to work
with the config.
Arne