Child pages
  • Wonder Logging

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: add a best practices paragraph

...

Code Block
languagejava
try {
  …
} catch (Exception e) {
  log.error("Error during processing of id {}", id, e);
}

Best Practices

Logging normally takes not part of the public API of a class and thus should be kept internally. So your class should neither use class-external logger objects nor provide own logger objects to other classes but declare them in private variables instead. Best practice is to create a logger that way:

Code Block
languagejava
class MyClass {
  private static final Logger log = LoggerFactory.getLogger(MyClass.class);
  …
}

If you want to use a logger associated to another class you can pass that class to the getLogger method. Logger objects are cached so you won't create multiple instances of logger objects for the same class.

More SLF4J info

For more info regarding SLF4J look at the introductory user manual or head to their documentation page.

...