java logging - technoangel/java GitHub Wiki

Logging in Java

Log4j

log4j is a popular logging system that is highly configurable and available for other languages. It supports internationalization, uses a config file to change log level at runtime, and uses multiple levels for granularity. Log4j does not guarantee that any log statement is delivered but it does implement fail-stop (By fail-stop, we mean that log4j will not throw unexpected exceptions at run-time potentially causing your application to crash.)

To get started, the Java project will require both an API and an implementation. Without the implementation, you will get a StaticLoggerBinding error. Initially we used slf4j as the API and slf4j-simple as the implementation. Later development efforts only changed the underlying implementation.

If you use log4j, you'll need to initialize it correctly or you will get an appender error - where is the log going to go? You need a default appender in a simple log4j property file (log4j.properties). We use slf4j as a facade that routes calls to log4j's implementation API.

Loggers

This is responsible for capturing logging information

Appenders

This is responsible for publishing logging information to preferred destinations - an output destination, for example.

There are a few important properties of any appender:

  • layout - PatternLayout, HTMLLayout, XMLLayout, this is the layout of the information sent to be logged.
  • target
  • level

There are also different categories of appender:

  • ConsoleAppender
  • FileAppender
  • RollingFileAppender
  • SocketAppender

Layouts

This is responsible for formatting logging information in different formats