The Simple Logging Facade for Java (SLF4J) serves as a simple abstraction for various logging frameworks. Let’s look at how to configure SLF4J to work with SLF4J Simple logger, JDK 1.4 logger, Log4j, Logback and Log4j2 framework.
Following is a Java code of our logging application LogApp. Note that it uses SLF4J API classes to do the logging. The jar file slf4j-api-1.7.12.jar is the only compile time dependency.
The output shows that SLF4J is missing a logger implementation. In the following we’ll demonstrate how to plug in different logging backends. The principle is always the same: include the logging framework implementation jars on the classpath, include the respective SLF4J adaptor jar on the classpath and provide a configuration file specific to the logging framework.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J Simple logger
The SLF4J comes with a Simple logger implemenation. Simple logger provides only basic functionality. It read its configuration from a simplelogger.properties file that must be included on the classpath. There’s no way to specify a different location of the configuration file.
The java.util.logging package was introduced in JDK 1.4. The default logging.properties configuration file that comes with JRE (jre/lib/logging.properties) specifies a ConsoleHandler that routes logging to System.err. There’s no way how to configure the JDK 1.4 logger to log to standard output instead of standard error output unless you do the configuration programmaticaly. You can specify the location of your JDK 1.4 logging configuration file in java.util.logging.config.file Java property.
jdk14.stderr.properties - Output to console
handlers=java.util.logging.ConsoleHandler.level=FINEjava.util.logging.ConsoleHandler.level=FINEjava.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter# message pattern works since Java 7java.util.logging.SimpleFormatter.format=%1$tT [%2$s] %4$s - %5$s %6$s%n
jdk14.file.properties - Output to file
handlers=java.util.logging.FileHandler.level=FINEjava.util.logging.FileHandler.level=FINEjava.util.logging.FileHandler.pattern=/tmp/logger.outjava.util.logging.FileHandler.append=truejava.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter# message pattern works since Java 7java.util.logging.SimpleFormatter.format=%1$tT [%2$s] %4$s - %5$s %6$s%n
The Log4j doesn’t log a single message for you unless you provide it with a proper configuration:
log4j:WARN No appenders could be found for logger (LogApp).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The following are the sample Log4j configuration files. Note that the log4j.configuration Java property that specifies the location of the configuration file must be a URL. In the example below the /tmp/log4j.stdout.properties location has to be prepended with file: to form a URL.
With no configuration provided Logback defaults to printing all log messages on the console standard output. The logback-classic jar package that comes with Logback includes the org.slf4j.impl.StaticLoggerBinder class that serves as an adaptor to SLF4J framework. Therefore no extra SLF4J adaptor jar is needed on the runtime classpath. You can specify the location of your Logback configuration file in the logback.configurationFile Java property.
With no configuration provided Log4j2 informs you that it logs only error messages to stdout. You can provide the location of your Log4j2 configuration file in the log4j.configurationFile Java property.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
22:09:13.052 [main] ERROR LogApp - Error message