Wiki

Clone wiki

Okapi / Logging_Best_Practices

Introduction

Some notions to consider when logging.

Useful web site: http://www.javacodegeeks.com/2011/01/10-tips-proper-application-logging.html

Best Practices

Do not use system prints for permanent production code. Commented or otherwise.

Add logging to a class like this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final Logger logger = LoggerFactory.getLogger(getClass());

For static class you need to do this:

private final static Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

It's important to use the slf4j message variables if possible:

LOGGER.debug("foo string: {}", foo.bar)

  • Try to be as specific as possible when logging WARNING and ERROR. Include important information, but don't overwhelm the log reader with diagnostic output. Extra diagnostics can be logged as DEBUG if more detailed information is needed.
  • Make sure that your logging statements do not introduce side effects that change the behavior of the running code.
  • test (unit and integration) diagnostic code should use TRACE
  • for blocks of test code use "if (LOGGER.isTRACE()) {....}"
  • diagnostic output for core code should use DEBUG. (also if (LOGGER.isDEBUG()) {....} for block of code)
  • INFO should never for debug.
  • WARNING is used when something has gone wrong but the process may continue.
  • ERROR is a major problem and should be looked at immediately. ERROR means mission critical use cannot be guaranteed and need to be resolved.

Markers

We should investigate the use of Markers for even more fined grained logging options. But feel free to use Markers if it helps in a specific case. They are ignored generally so are safe to add without breaking standard logging.

See: http://logging.apache.org/log4j/2.x/manual/markers.html

Updated