Simple Logging Facade for Java (SLF4J) provides a Java logging API by means of a simple facade pattern.
Overview
The SLF4J implementation uses the latest OSGi framework API to provide the most convenient solution. Just like ref.Log it defines two logging levels:
There is a global configuration, that is applied to all loggers by default.
An individually separated log configuration (CM) will also be created for each bundle using the SLF4J API. This allows configuring of the bundle logging levels and specifying fine-grained log levels per class. For these configurations a dynamic meta-typing provider will be registered for easily properties editing from the Web Admin Console.
The bundle using the SLF4J API does not need to track the log service, the configuration admin or the metatype service. Any updates to the logging configuration are transparent for the using bundle. This means that no bundle restart is required to change the log levels.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Wombat {
final Logger logger = LoggerFactory.getLogger(Wombat.class);
Integer t;
Integer oldT;
public void setTemperature( Integer temperature) {
oldT = t;
t = temperature;
logger.debug( "Temperature set to {0}. Old temperature was {1}.", t, oldT);
if(temperature.intValue() > 50) {
logger.info( "Temperature has risen above 50 degrees.");
}
}
}
Registering Bundle
The service is registered by OSGi Log SLF4J Bundle.
Low Overhead and High Performance
Our SLF4J implementation uses the logging API used internally by the framework. That API is proven to work in very constrained environments with very high performance. The same features are inherited by the SLF4J implementation:
Features
Managed Diagnostic Context (MDC)
MDC allows the user to include some thread-specific associations in the log entry. The following MDCs are automatically supported by the implementation:
Log Entry Origin
Log service is disabled by default, but if enabled it will try to detect the exact location where the entry was logged from. That information includes the class name and the method name from which the entry was logged. In addition it can include also the line number in the source file, if the class was compiled with line numbers debug information.
Line numbers require introspection of the current stack trace and might significantly slow down the logging. So do not enable it in production environment. Also if you use the ref.Log utility it will not detect correctly the class and method in your bundle, but instead will report the method in ref.Log which called the logging service.