With jul-to-slf4j on the classpath, how do you install SLF4JBridgeHandler so java.util.logging records reach SLF4J, and how are JUL levels mapped?
answer
- a handler on the JUL root logger
- remove the default handlers first
- or one line in logging.properties
- CONFIG lands one level up from DEBUG
- JUL filters before the handler sees it
basics
~10 sCall SLF4JBridgeHandler.removeHandlersForRootLogger(), then SLF4JBridgeHandler.install(), once at startup, or name the handler in logging.properties. FINEST maps to TRACE, FINER and FINE to DEBUG, CONFIG and INFO to INFO, WARNING to WARN, SEVERE to ERROR.
solid answer
~40 s`jul-to-slf4j` ships `SLF4JBridgeHandler`, a `java.util.logging.Handler` that turns each JUL `LogRecord` into an SLF4J call. Programmatically you call `SLF4JBridgeHandler.removeHandlersForRootLogger()` (since 1.6.5) so the root logger's existing handlers stop printing the same records, then `SLF4JBridgeHandler.install()`, which adds one bridge to the JUL root logger. The file route is `handlers = org.slf4j.bridge.SLF4JBridgeHandler` in `logging.properties`. Levels map by integer threshold: `FINEST` to TRACE, `FINER` and `FINE` to DEBUG, `CONFIG` and `INFO` to INFO, `WARNING` to WARN, anything higher to ERROR. The handler only sees records JUL itself let through: a call below the JUL logger's own level is dropped inside JUL and never reaches SLF4J, whatever the SLF4J backend is set to.
code
java · 12 linesimport org.slf4j.bridge.SLF4JBridgeHandler;
public final class JulBridge {
private JulBridge() {}
public static void installOnce() {
if (!SLF4JBridgeHandler.isInstalled()) {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
}
}go deeper
Recall that jul-to-slf4j needs an install step, the two method names in order, and that JUL's levels have different names from SLF4J's.
Explain that install() adds a handler to the JUL root logger, why removing the root handlers first avoids duplicates, and where CONFIG and FINER land in the mapping.
Show you know JUL filters before the bridge sees anything, that install() is not idempotent, and that JUL parameters arrive pre-formatted with MessageFormat rather than SLF4J placeholders.
Frame the bridge as one shared start-up concern: one owner installs it, everyone else checks isInstalled(), and JUL levels are set deliberately rather than left to defaults.
## What the JUL bridge is **java.util.logging** (JUL) is the logging API built into the JDK. Libraries and JDK components that log through it write to `java.util.logging.Logger` objects, and JUL hands each accepted event, a `LogRecord`, to the **handlers** attached to that logger and its parents. The `jul-to-slf4j` jar adds one more handler, `org.slf4j.bridge.SLF4JBridgeHandler`. Its job is one-directional: it takes a JUL record and re-issues it through the **SLF4J API**, so it ends up in whatever SLF4J provider the application has bound. Its own javadoc describes the goal as installing it on the root logger "as the sole JUL handler in the system". Unlike `jcl-over-slf4j` or `log4j-over-slf4j`, this bridge cannot replace the JUL classes, because they live in the JDK. That is why it needs an explicit install step. In a service that also runs a JDK component or an old library logging through JUL, this bridge is what brings those lines into the same output, format and level configuration as the application's own SLF4J calls; without it they go wherever JUL's own handlers send them, typically the console in JUL's default format. ## Installing it in code The javadoc's recipe, run **once**, early in application start-up: 1. `SLF4JBridgeHandler.removeHandlersForRootLogger()` (since SLF4J 1.6.5) detaches every handler currently on the JUL root logger. The JDK's default configuration normally attaches a console handler there; leaving it means every JUL line is printed twice, once by JUL and once by your SLF4J backend. 2. `SLF4JBridgeHandler.install()` calls `LogManager.getLogManager().getLogger("").addHandler(new SLF4JBridgeHandler())`, that is, it adds a new bridge to the root logger. Two helpers complete the set: - `SLF4JBridgeHandler.isInstalled()` returns true when a bridge is already on the root logger. - `SLF4JBridgeHandler.uninstall()` removes only the bridge instances, leaving other handlers alone. `install()` does not check `isInstalled()` itself, so calling it twice attaches two bridges and every JUL record is logged twice. When more than one piece of start-up code might install it (a framework may already do so), guard the call. ## Installing it from logging.properties JUL can also be configured by file. The javadoc's line is `handlers = org.slf4j.bridge.SLF4JBridgeHandler`, which registers the bridge as the root logger's handler. Because your own `logging.properties` takes the place of the JDK's default file, its `handlers` line defines the root logger's handlers outright: no console handler is attached unless you list one. ## How levels map The handler compares the record's integer level against JUL's own constants, so the mapping covers every level, including `CONFIG` and custom ones: | JUL level (int) | SLF4J level | |---|---| | `FINEST` (300) | TRACE | | `FINER` (400) | DEBUG | | `FINE` (500) | DEBUG | | `CONFIG` (700) | INFO | | `INFO` (800) | INFO | | `WARNING` (900) | WARN | | `SEVERE` (1000) | ERROR | The javadoc's table omits `CONFIG`; the code's thresholds (`<= Level.INFO.intValue()`) send it to INFO. A custom level falls into whichever band its integer value lands in. ## What reaches the handler and what does not JUL decides first. A `fine()` call on a JUL logger whose effective level is `INFO` is rejected inside `java.util.logging.Logger` before any `LogRecord` reaches a handler. The `install()` javadoc says it directly: a disabled JUL statement's non-event "will not reach SLF4JBridgeHandler and cannot be redirected". Your SLF4J backend's level only filters what JUL already accepted. `publish()` itself ignores the record's level for filtering, leaving that decision to SLF4J. One more JUL rule matters: the bridge lives on the **root** logger, so it only sees records that propagate up to the root. A JUL logger configured with `setUseParentHandlers(false)` keeps its records to its own handlers, and they never reach the bridge. ## How the message is translated - A `null` logger name becomes `unknown.jul.logger`. - A `null` message becomes the empty string, because other frameworks do not accept null. - If the record carries a `ResourceBundle`, the message is looked up in it first. - JUL parameters use `java.text.MessageFormat` style (`{0}`, `{1}`), not SLF4J's `{}`; the bridge formats them with `MessageFormat.format` and keeps the raw text if formatting fails. - `record.getThrown()` is passed as the SLF4J throwable, and when the SLF4J logger is location-aware, the bridge passes `java.util.logging.Logger` as the caller boundary so caller data points at the code that called JUL. ## Checklist - Install once, early, and guard with `isInstalled()`. - Remove the root handlers first, or accept duplicate console lines. - Lower a JUL logger's level when you want its fine-grained records; the SLF4J side cannot ask for what JUL dropped. - Never bind SLF4J to `slf4j-jdk14` while the bridge is installed: records would travel JUL to SLF4J to JUL and back until the stack overflows.
- Why does the recipe call removeHandlersForRootLogger() before install() instead of install() alone?`install()` only adds a bridge; it leaves whatever the root logger already had. The JDK's default configuration normally puts a console handler there, so without the removal each JUL record is printed by that handler and also sent to SLF4J, giving duplicate lines in two formats. `removeHandlersForRootLogger()` detaches all root handlers so the bridge is the only one.
- A JUL message reads "Loaded {0} entries" with one parameter. How does it look after crossing SLF4JBridgeHandler?The bridge formats it itself with `java.text.MessageFormat.format` before calling SLF4J, so the backend receives "Loaded 42 entries" as a finished string; SLF4J's `{}` placeholder rules play no part. If the record carries a `ResourceBundle`, the message is first looked up as a key. If `MessageFormat` rejects the pattern, the raw text is logged unchanged.
- Is calling SLF4JBridgeHandler.install() twice harmless?No. `install()` adds a new handler every time and does not consult `isInstalled()`, so two calls leave two bridges on the root logger and every JUL record is logged twice. Guard the call with `SLF4JBridgeHandler.isInstalled()`, or use `uninstall()`, which removes only bridge instances, before installing again.
- Calling install() alone stops JUL's own console output.
- Setting only the SLF4J backend's level to DEBUG lets JUL fine() calls through.
- The bridge formats JUL parameters with SLF4J's {} placeholders.
- JUL's CONFIG level arrives in SLF4J as DEBUG.
- install() attaches the bridge to every existing JUL logger individually.
- Calling install() a second time is a harmless no-op.