Java Reference
In-Depth Information
SLF4J provides a common API using a simple facade pattern that abstracts the un-
derlying logging implementation. SLF4J can be used with most of the common logging
frameworks, such as the Java Logging API ( java.util.logging ), Log4j, Jakarta
Commons Logging, and others. In practice, SLF4J provides the flexibility to choose
(and swap) logging frameworks and allows projects that use SLF4J to quickly become
integrated into an application's selected logging framework.
To use SLF4J in an application, download the SLF4J binaries located at ht-
tp://www.slf4j.org/ . Once they're downloaded, extract the contents and add
slf4j-api-x.x.x.jar to the project. This is the main .jar file that contains the
SLF4J API (on which a program can call to log information). After adding the
slf4j-api-x.x.x.jar file to the project, find slf4j-jdk14-x.x.x.jar
and add that to the project. This second file indicates that SLF4j will use the
java.util.logging classes to log information.
The way SLF4J works is that at runtime SLF4J scans the class path and picks the
first .jar that implements the SLF4J API. In the example case, the slf4j-
jdk14-x.x.x.jar is found and loaded. This .jar represents the native Java Log-
ging Framework (known as jdk.1.4 logging). If, for example, you wanted to use an-
other logging framework, replace slf4j-jdk14-x.x.x.jar with the correspond-
ing SLF4J implementation for the desired logger. For example, to use Apache's Log4J
logging framework, include slf4j-log4j12-x.x.x.jar .
Note The java.util.logging framework is configured by the properties log
file.
Once SLF4J is configured, you can log information in your application by calling
the SLF4J logging methods. The methods log information depending on the logging
level. The logging level can then be used to filter which messages are actually logged.
The ability to filter messages by log level is useful because there may be a lot of in-
formational or debugging information being logged. If there is the need to troubleshoot
an application, the logging level can be changed, and more information can be made
visible in the logs without changing any code. The ability to filter messages through
their level is referred to as setting the log level . Each logging framework reference con-
tains its own configuration file that sets the log level (among other things, such as the
logging file name and logging-file configurations). In the example case, because SLF4j
is using the java.util.logging framework to log, you would need to configure
the java.util.logging properties for the desired logging. See Table 9-1 .
 
Search WWH ::




Custom Search