Top Description Fields Constructors Methods
org.slf4j.bridge

public Class SLF4JBridgeHandler

extends Handler
Class Inheritance
Imports
java.text.MessageFormat, java.util.MissingResourceException, .ResourceBundle, java.util.logging.Handler, .Level, .LogManager, .LogRecord, org.slf4j.Logger, .LoggerFactory, org.slf4j.spi.LocationAwareLogger

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 of the following conditions is true:

  1. few j.u.l. logging statements are in play
  2. LevelChangePropagator has been installed

As a Java 9/Jigsaw module

Given that to is a reserved keyword under Java 9 within module productions, the MANIFEST.MF file in jul-to-slf4j.jar declares jul_to_slf4j as its Automatic Module Name. Thus, if your application is Jigsaw modularized, the requires statement in your module-info.java needs to be jul_to_slf4j (note the two underscores).

Authors
Christian Stein, Joern Huxhorn, Ceki Gülcü, Darryl Smith
Since
1.5.1

Field Summary

Modifier and TypeField and Description
private static final int
private static final String
private static final int
private static final int
private static final String
private static final int

Constructor Summary

AccessConstructor and Description
public
SLF4JBridgeHandler()

Initialize this handler.

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record)

public void
close()

Implements abstract java.util.logging.Handler.close.

No-op implementation.
public void
flush()

Implements abstract java.util.logging.Handler.flush.

No-op implementation.
private String
getMessageI18N(LogRecord record)

Get the record's message, possibly via a resource bundle.

private static Logger
protected Logger

Returns:

an SLF4J logger corresponding to the record parameter's logger name
getSLF4JLogger
(LogRecord
a LogRecord
record
)

Return the Logger instance that will be used for logging.

public static void
install()

Adds a SLF4JBridgeHandler instance to jul's root logger.

public static boolean

Returns:

true if SLF4JBridgeHandler is already installed, false otherwise
isInstalled
()

Returns true if SLF4JBridgeHandler has been previously installed, returns false otherwise.

public void
publish(LogRecord
Description of the log event. A null record is silently ignored and is not published.
record
)

Implements abstract java.util.logging.Handler.publish.

Publish a LogRecord.
public static void
removeHandlersForRootLogger()

Invoking this method removes/unregisters/detaches all handlers currently attached to the root logger

public static void
uninstall()

Removes previously installed SLF4JBridgeHandler instances.

Inherited from java.util.logging.Handler:
getEncodinggetErrorManagergetFiltergetFormattergetLevelisLoggablereportErrorsetEncodingsetErrorManagersetFiltersetFormattersetLevel

Field Detail

DEBUG_LEVEL_THRESHOLDback to summary
private static final int DEBUG_LEVEL_THRESHOLD
FQCNback to summary
private static final String FQCN
INFO_LEVEL_THRESHOLDback to summary
private static final int INFO_LEVEL_THRESHOLD
TRACE_LEVEL_THRESHOLDback to summary
private static final int TRACE_LEVEL_THRESHOLD
UNKNOWN_LOGGER_NAMEback to summary
private static final String UNKNOWN_LOGGER_NAME
WARN_LEVEL_THRESHOLDback to summary
private static final int WARN_LEVEL_THRESHOLD

Constructor Detail

SLF4JBridgeHandlerback to summary
public SLF4JBridgeHandler()

Initialize this handler.

Method Detail

callLocationAwareLoggerback to summary
protected void callLocationAwareLogger(LocationAwareLogger lal, LogRecord record)
callPlainSLF4JLoggerback to summary
protected void callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record)
closeback to summary
public void close()

Implements abstract java.util.logging.Handler.close.

No-op implementation.

flushback to summary
public void flush()

Implements abstract java.util.logging.Handler.flush.

No-op implementation.

getMessageI18Nback to summary
private String getMessageI18N(LogRecord record)

Get the record's message, possibly via a resource bundle.

getRootLoggerback to summary
private static Logger getRootLogger()
getSLF4JLoggerback to summary
protected Logger getSLF4JLogger(LogRecord record)

Return the Logger instance that will be used for logging.

Parameters
record:LogRecord

a LogRecord

Returns:Logger

an SLF4J logger corresponding to the record parameter's logger name

installback to summary
public static void install()

Adds a SLF4JBridgeHandler instance to jul's root logger.

This handler will redirect j.u.l. logging to SLF4J. However, only logs enabled in j.u.l. will be redirected. For example, if a log statement invoking a j.u.l. logger is disabled, then the corresponding non-event will not reach SLF4JBridgeHandler and cannot be redirected.

isInstalledback to summary
public static boolean isInstalled()

Returns true if SLF4JBridgeHandler has been previously installed, returns false otherwise.

Returns:boolean

true if SLF4JBridgeHandler is already installed, false otherwise

publishback to summary
public void publish(LogRecord record)

Implements abstract java.util.logging.Handler.publish.

Publish a LogRecord.

The logging request was made initially to a Logger object, which initialized the LogRecord and forwarded it here.

This handler ignores the Level attached to the LogRecord, as SLF4J cares about discarding log statements.

Parameters
record:LogRecord

Description of the log event. A null record is silently ignored and is not published.

removeHandlersForRootLoggerback to summary
public static void removeHandlersForRootLogger()

Invoking this method removes/unregisters/detaches all handlers currently attached to the root logger

Since
1.6.5
uninstallback to summary
public static void uninstall() throws SecurityException

Removes previously installed SLF4JBridgeHandler instances. See also install().

Exceptions
SecurityException:
A SecurityException is thrown, if a security manager exists and if the caller does not have LoggingPermission("control").