From: Martin Desruisseaux <martin.desruisseaux@te...> - 2002-08-21 09:32:09
Thinking again about it... There is some more points that I would like
to share in favor of package level (as opposite to class level) logger
1) Our package structure doesn't change much lately. In comparaison, we
have added, renamed, moved or deleted classes more often. Allowing
the user to control logging for individual classes is like allowing
to control logging for a moving target. We may have more freedom if
we limit ourself to the more conservative package level.
2) It seems to me that one argument for class level loggers was to
keep trace of the originating class for each log message. But this
functionality is already provided in J2SE without the need for a
different logger for each class. The LogRecord class has
getSourceClassName() and getSourceMethodName() methods. Actually,
every time Logger.fine(String)' and its friend is invoked, the J2SE
logging framework automatically take note of the calling class and
method. So we have this information anyway, no matter which logger
As a side note, Sun uses native code for fetching the caller's class
and method name. It would not have been possible to use native code
in a safe environment if the logging framework wasn't bundle with
J2SE, so I'm not sure if Log4J can provides the same functionality...
3) Since we have hundred of classes, one logger per class would make a
lot of loggers. Futhermore, its look like that Sun (and maybe Log4J
too) suggests a package name level.
4) A logger per class is a very fine level (and a moving target as said
earlier). If a user want to control the logging at such a detailed
level, a better approach way be to use an java.util.logging.Filter
instead? As a see it, package name for logger is a good compromise
between name stability and control for most uses. If more control
is wanted, then java.util.logging.Filter is the way to go.
5) If a class produces a lot of log message which are very specific to
the class task, maybe it would be better to create a new Level
instead of a new Logger for this class? For example, if a particular
class generate a log message every time a SQL statement is executed,
it may be worth to create a system-wide SQL_QUERY or SQL_UPDATE level
instead of creating a new logger for this class only?