What are the best practices to log an error?

12,461

Solution 1

Apache Commons Logging is not intended for applications general logging. It's intended to be used by libraries or APIs that don't want to force a logging implementation on the API's user.

There are also classloading issues with Commons Logging.

Pick one of the [many] logging api's, the most widely used probably being log4j or the Java Logging API.

If you want implementation independence, you might want to consider SLF4J, by the original author of log4j.

Having picked an implementation, then use the logging levels/severity within that implementation consistently, so that searching/filtering logs is easier.

Solution 2

Logging directly to the console is horrendous and frankly, the mark of an inexperienced developer. The only reason to do this sort of thing is 1) he or she is unaware of other approaches, and/or 2) the developer has not thought one bit about what will happen when his/her code is deployed to a production site, and how the application will be maintained at that point. Dealing with an application that is logging 1GB/day or more of completely unneeded debug logging is maddening.

The generally accepted best practice is to use a Logging framework that has concepts of:

  1. Different log objects - Different classes/modules/etc can log to different loggers, so you can choose to apply different log configurations to different portions of the application.
  2. Different log levels - so you can tweak the logging configuration to only log errors in production, to log all sorts of debug and trace info in a development environment, etc.
  3. Different log outputs - the framework should allow you to configure where the log output is sent to without requiring any changes in the codebase. Some examples of different places you might want to send log output to are files, files that roll over based on date/size, databases, email, remoting sinks, etc.
  4. The log framework should never never never throw any Exceptions or errors from the logging code. Your application should not fail to load or fail to start because the log framework cannot create it's log file or obtain a lock on the file (unless this is a critical requirement, maybe for legal reasons, for your app).

The eventual log framework you will use will of course depend on your platform. Some common options:

Solution 3

The easiest way to log errors in a consistent format is to use a logging framework such as Log4j (assuming you're using Java). It is useful to include a logging section in your code standards to make sure all developers know what needs to be logged. The nice thing about most logging frameworks is they have different logging levels so you can control how verbose the logging is between development, test, and production.

Solution 4

A best practice is to use the java.util.logging framework

Then you can log messages in either of these formats

log.warning("..");
log.fine("..");
log.finer("..");
log.finest("..");

Or

log.log(Level.WARNING, "blah blah blah", e);

Then you can use a logging.properties (example below) to switch between levels of logging, and do all sorts of clever stuff like logging to files, with rotation etc.

handlers = java.util.logging.ConsoleHandler

.level = WARNING

java.util.logging.ConsoleHandler.level = ALL

com.example.blah = FINE
com.example.testcomponents = FINEST

Frameworks like log4j and others should be avoided in my opinion, Java has everything you need already.

EDIT

This can apply as a general practice for any programming language. Being able to control all levels of logging from a single property file is often very important in enterprise applications.

Solution 5

Some suggested best-practices

  • Use a logging framework. This will allow you to:

    • Easily change the destination of your log messages
    • Filter log messages based on severity
    • Support internationalised log messages
  • If you are using java, then slf4j is now preferred to Jakarta commons logging as the logging facade.

  • As stated slf4j is a facade, and you have to then pick an underlying implementation. Either log4j, java.util.logging, or 'simple'.

  • Follow your framework's advice to ensuring expensive logging operations are not needlessly carried out

Share:
12,461

Related videos on Youtube

Agusti-N
Author by

Agusti-N

Updated on April 19, 2022

Comments