Re: When to log? (best practices and localization)
Karsten Wutzke wrote:
When do you actually make use of some logging tool generally? I need
some motivation and benefits. Sure logging is a must for most/all
server apps as well as well as apps that have no console to print to.
Sooner or later any software will be deployed and mostly all apps
won't have a console available. So the short answer would probably be
to use a logging tool whenever you can over any System.out or
System.err prints, but this seems to include (too) much overhead. Is
it?
I'm rather vague on what I really want to ask here, I simply don't
know when to use a logging tool and when System prints are "enough".
Always use a logging tool. System prints are never "enough"; they are always
too much. Always use a logging tool, Always.
I have set up my IDE templates for Java classes to include the import of the
logging library and declaration of a "logger" instance variable.
As to what to log, it's more important to have a logging strategy at all than
what that strategy is.
To develop your stratgey, try living with operations for a week, and see what
information they lack as stakeholders demand action. You will quickly
discover what they wish they could glean from log files, but never can.
Then when designing logging strategies, think like the sysadmin who has to
diagnose and repair, not to say monitor and optimize the application in
production.
Obviously errors must be logged with the appropriate level (ERROR, SEVERE,
whatever equivalent your logger sports). Errors are those things that stop
the program from working correctly or at all.
Anomalies that are recoverable should be logged at WARN level.
Useful diagnostic information that isn't critical should be at INFO level.
Details of what a statement or method accomplishes, low-level details of
exceptions and other voluminous output should log at DEBUG level.
The more detailed the log level, the more such logging output there can be.
You might have zillions of lines of DEBUG output but just a basic "oops"
message at ERROR level. (Or you might prefer to dump all relevant information
at ERROR on the notion that it's a rare occurrence.) INFO output can be quite
verbose, as the system usually will not log such details until the sysadmin
needs it to.
The salient principle for logging is that it's there to help in production.
Make sure that it does.
--
Lew