Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commons Logging priority best practices

This may be a purely subjective question (if no organization has attempted to standardize this), but my team struggles with this more than you would think.

We use Apache Commons Logging as our logging interface and often the use of priority is not consistent across our development team. For example, some developers log any caught exception to fatal (log.fatal(message)) even though the flow is able to handle the error whereas others only log to fatal when something cause the program to necessarily cease execution for whatever reason.

I would like to know how other teams define each priority. Does anyone work at a company that explicitly tries to define best practices for this? Has Jakarta weighed in on this?

My goal would be to send a simple recommendation for each priority out to my whole team so that we can more effectively handle our unwieldy application logging in a consistent fashion.

like image 571
smp7d Avatar asked Sep 20 '11 14:09

smp7d


2 Answers

I have always used this as a guideline; I use Log4j more than Commons Logging but this may still be helpful:

DEBUG - for genuinely debug-level info; will not be seen in production or shipped product, as INFO will be the minimum level; good for capturing timings, number of occurrences of events, etc

INFO - minimum level for production/shipped usage; record data likely to be useful in forensic investigations and to confirm successful outcomes ("stored 999 items in DB OK"); all info here must be such that you would be OK with end users/customers seeing it and sending you it, if need be (no secrets, no profanity!)

WARN - not an error level as such, but useful to know the system may be entering dodgy territory, e.g. business logic stuff like "number of ordered products < 0" which suggests a bug somewhere, but isn't a system exception; I tend not to use it that much to be honest, finding things tend to be more natural fits to INFO or ERROR

ERROR - use this for exceptions (unless there's a good reason to reduce to WARN or INFO); log full stacktraces along with important variable values without which diagnosis is impossible; use only for app/system errors, not bad business logic circumstances

FATAL - only use this for an error of such high severity that it literally prevents the app from starting / continuing

Additionally - review your logging often, both with DEBUG and INFO levels active, you want to be able to follow a good narrative through, with prominent events easy to find, and to make sure you're not doing anything too verbose which detracts from readability.

Also ensure you use a pattern which leads to neat columns for things like timestamps, source-classes and severity - again, it helps readability massively.

like image 161
Brian Avatar answered Sep 30 '22 07:09

Brian


Here's what we use (and I'd say many others as well):

  • FATAL: errors that endanger the consistency of the system and might require an immediate shutdown/restart - very rarely manually used
  • ERROR: exceptions that should not be thrown and represent a bug in the system (normally all exceptions that are not caught up to a certain point)
  • WARN: exceptions that might occur but are accounted for or situations that might imply a logic/usage error - we decide whether to track those or not
  • INFO: whatever you need to have in the log, e.g. what users currently do (in our case of web applications: what page the user is navigating to etc.)
  • DEBUG: debug only messages like timings etc, generally turned off in the logs
  • TRACE: we don't use that, you might use it for even more specific debugging info

In some cases we start with logging messages as ERROR, in order to get the notification when something we'd normally not like to happen occurs. Later, if we decide that the source for that error can not be removed, we handle it and change the log level to WARN.

Note that our test and production systems are set up to send an email for FATAL and ERROR. Thus we don't have to check the log files for those manually but only have to check one email inbox.

Hope that helps.

Edit: did you already have a look at the Apache Commons Logging best pratices?

like image 37
Thomas Avatar answered Sep 30 '22 09:09

Thomas