On Tue, 2006-03-07 at 21:10 +0100, Martin Marinschek wrote:
> Hi *, Hi Simon,
>
> logging again - what do we do with static methods - do we have to get
> all of them out?
>
> or should we be calling getLog on every logging attempt?
Yep, even static methods must avoid logging via a static logger :-(
In general, calling LogFactory.getLog within the static method would be
the appropriate solution:
public static void doStuff(...) {
Log log = LogFactory.getLog("stuff");
log.warn(...);
}
If the static method has a reference to some object then it could
retrieve a logger from it, eg
public static void doStuff(AppContext context, ...) {
context.getStuffLog().warn("oops");
}
This doesn't seem widely applicable though.
Fancier solutions like retrieving log objects from a collection in a
thread-local variable are probably not worth trying; the
LogFactory.getLog method isn't that slow.
I've created a wiki page with more info on this subject if you're
interested:
http://wiki.apache.org/jakarta-commons/Logging/StaticLog
Note in particular that both SLF4J and java.util.logging have exactly
this same problem when used with an underlying logging library that is
container-aware, so avoiding static loggers is something that really
should be done anyway.
Cheers,
Simon