public class SLF4JBridgeHandler
extends java.util.logging.Handler
Bridge/route all JUL log records to the SLF4J API.
Essentially, the idea is to install on the root logger an instance of
SLF4JBridgeHandler
as the sole JUL handler in the system. Subsequently, the
SLF4JBridgeHandler instance will redirect all JUL log records are redirected
to the SLF4J API based on the following mapping of levels:
FINEST -> TRACE FINER -> DEBUG FINE -> DEBUG INFO -> INFO WARNING -> WARN SEVERE -> ERROR
Programmatic installation:
// Optionally remove existing handlers attached to j.u.l root logger SLF4JBridgeHandler.removeHandlersForRootLogger(); // (since SLF4J 1.6.5) // add SLF4JBridgeHandler to j.u.l's root logger, should be done once during // the initialization phase of your application SLF4JBridgeHandler.install();
Installation via logging.properties configuration file:
// register SLF4JBridgeHandler as handler for the j.u.l. root logger handlers = org.slf4j.bridge.SLF4JBridgeHandler
Once SLF4JBridgeHandler is installed, logging by j.u.l. loggers will be directed to SLF4J. Example:
import java.util.logging.Logger; ... // usual pattern: get a Logger and then log a message Logger julLogger = Logger.getLogger("org.wombat"); julLogger.fine("hello world"); // this will get redirected to SLF4J
Please note that translating a java.util.logging event into SLF4J incurs the
cost of constructing LogRecord
instance regardless of whether the
SLF4J logger is disabled for the given level. Consequently, j.u.l. to
SLF4J translation can seriously increase the cost of disabled logging
statements (60 fold or 6000% increase) and measurably impact the performance of enabled log
statements (20% overall increase). Please note that as of logback-version 0.9.25,
it is possible to completely eliminate the 60 fold translation overhead for disabled
log statements with the help of LevelChangePropagator.
If you are concerned about application performance, then use of SLF4JBridgeHandler
is appropriate only if any one the following two conditions is true:
Modifier and Type | Field and Description |
---|---|
private static int |
DEBUG_LEVEL_THRESHOLD |
private static java.lang.String |
FQCN |
private static int |
INFO_LEVEL_THRESHOLD |
private static int |
TRACE_LEVEL_THRESHOLD |
private static java.lang.String |
UNKNOWN_LOGGER_NAME |
private static int |
WARN_LEVEL_THRESHOLD |
Constructor and Description |
---|
SLF4JBridgeHandler()
Initialize this handler.
|
Modifier and Type | Method and Description |
---|---|
protected void |
callLocationAwareLogger(LocationAwareLogger lal,
java.util.logging.LogRecord record) |
protected void |
callPlainSLF4JLogger(Logger slf4jLogger,
java.util.logging.LogRecord record) |
void |
close()
No-op implementation.
|
void |
flush()
No-op implementation.
|
private java.lang.String |
getMessageI18N(java.util.logging.LogRecord record)
Get the record's message, possibly via a resource bundle.
|
private static java.util.logging.Logger |
getRootLogger() |
protected Logger |
getSLF4JLogger(java.util.logging.LogRecord record)
Return the Logger instance that will be used for logging.
|
static void |
install()
Adds a SLF4JBridgeHandler instance to jul's root logger.
|
static boolean |
isInstalled()
Returns true if SLF4JBridgeHandler has been previously installed, returns false otherwise.
|
void |
publish(java.util.logging.LogRecord record)
Publish a LogRecord.
|
static void |
removeHandlersForRootLogger()
Invoking this method removes/unregisters/detaches all handlers currently attached to the root logger
|
static void |
uninstall()
Removes previously installed SLF4JBridgeHandler instances.
|
private static final java.lang.String FQCN
private static final java.lang.String UNKNOWN_LOGGER_NAME
private static final int TRACE_LEVEL_THRESHOLD
private static final int DEBUG_LEVEL_THRESHOLD
private static final int INFO_LEVEL_THRESHOLD
private static final int WARN_LEVEL_THRESHOLD
public static void install()
private static java.util.logging.Logger getRootLogger()
public static void uninstall() throws java.lang.SecurityException
install()
.java.lang.SecurityException
- A SecurityException
is thrown, if a security manager
exists and if the caller does not have
LoggingPermission("control").public static boolean isInstalled() throws java.lang.SecurityException
java.lang.SecurityException
public static void removeHandlersForRootLogger()
public void close()
close
in class java.util.logging.Handler
public void flush()
flush
in class java.util.logging.Handler
protected Logger getSLF4JLogger(java.util.logging.LogRecord record)
protected void callLocationAwareLogger(LocationAwareLogger lal, java.util.logging.LogRecord record)
protected void callPlainSLF4JLogger(Logger slf4jLogger, java.util.logging.LogRecord record)
private java.lang.String getMessageI18N(java.util.logging.LogRecord record)
record
- public void publish(java.util.logging.LogRecord record)
publish
in class java.util.logging.Handler
record
- Description of the log event. A null record is silently ignored
and is not published.