A Spring Boot service embeds one library logging through Log4j 1 and another through the Log4j 2 API; which Log4j-related SLF4J jars do you ship when the backend is Logback, and when it is Log4j 2?
answer
- every jar points one way
- API in, provider out
- the backend picks the set
- slf4j-log4j12 is now a relocation
- reload4j checks for log4j-over-slf4j
basics
~20 sOn Logback, ship log4j-over-slf4j for Log4j 1 calls and log4j-to-slf4j for Log4j 2 API calls. On Log4j 2, keep log4j-over-slf4j but bind SLF4J with log4j-slf4j2-impl and drop log4j-to-slf4j. Never pair a jar with its reverse.
solid answer
~40 sEach jar carries calls one way. `log4j-over-slf4j` re-implements the Log4j 1 API on top of SLF4J; `slf4j-reload4j` is the opposite, an SLF4J provider writing to reload4j, and `slf4j-log4j12` is now only a relocation pom pointing at it. For Log4j 2, `log4j-to-slf4j` carries Log4j 2 API calls into SLF4J, while `log4j-slf4j2-impl` is an SLF4J 2 provider writing to Log4j 2. With Logback bound you want both inbound jars, `log4j-over-slf4j` and `log4j-to-slf4j`, and neither provider. With Log4j 2 as the backend you bind SLF4J with `log4j-slf4j2-impl`, keep `log4j-over-slf4j` for the old library, and remove `log4j-to-slf4j`, which would close a loop. `slf4j-reload4j` fails fast if it finds `log4j-over-slf4j`; the Log4j 2 pair gets no such check from SLF4J's own source.
code
console · 2 linesDetected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.go deeper
Recall that some jars carry another API's calls into SLF4J and others carry SLF4J out to a backend, and that their names tell you which way.
Explain the four Log4j artifacts by direction, that log4j-over-slf4j replaces the org.apache.log4j classes, and that slf4j-log4j12 now relocates to slf4j-reload4j.
Pick the jar set from the backend in use, explain the reload4j static guard and its message, and warn that log4j-over-slf4j silently ignores a library's own Log4j 1 configuration.
Argue for one declared backend per service, with the reverse jars banned by a build rule, since SLF4J's own guard covers only one of the possible loops.
## Direction is the whole question SLF4J's family of jars splits into two kinds. A **bridge** (inbound adapter) takes calls made to some other logging API and re-issues them through the SLF4J API. A **provider** (or, in 1.x terms, a binding) sits under the SLF4J API and writes SLF4J's events into one concrete backend. A healthy classpath is a set of arrows that all end at exactly one backend. The trouble starts when one jar carries calls from API X into SLF4J and another carries SLF4J back into X: every event then circles until the stack overflows. The four Log4j-related artifacts, with their direction: | Artifact | Calls go from | Calls go to | Who publishes it | |---|---|---|---| | `log4j-over-slf4j` | Log4j 1 API (`org.apache.log4j.*`) | SLF4J | SLF4J | | `slf4j-reload4j` | SLF4J | reload4j, a Log4j 1.2-compatible backend | SLF4J | | `log4j-to-slf4j` | Log4j 2 API | SLF4J | Log4j 2 | | `log4j-slf4j2-impl` | SLF4J 2 | Log4j 2 | Log4j 2 | ## Choosing by backend The question to ask is which backend the service runs, then pick every arrow that ends there. **Logback bound as the SLF4J provider:** - add `log4j-over-slf4j` so the old library's `org.apache.log4j.Logger` calls reach SLF4J; - add `log4j-to-slf4j` so Log4j 2 API calls reach SLF4J; - remove the real Log4j 1 or reload4j jar, since `log4j-over-slf4j` supplies the same `org.apache.log4j` classes; - keep `slf4j-reload4j` and `log4j-slf4j2-impl` off the classpath, because they would be second providers pointing away from Logback. **Log4j 2 as the backend:** - bind SLF4J with `log4j-slf4j2-impl`; - Log4j 2 API calls need no jar, they are already native; - Log4j 1 calls can still go through `log4j-over-slf4j` into SLF4J and from there into Log4j 2 (Log4j 2 also publishes its own Log4j 1 bridge, which is outside SLF4J's scope); - remove `log4j-to-slf4j`: with `log4j-slf4j2-impl` present it closes the loop Log4j 2 to SLF4J to Log4j 2. **reload4j as the backend** (a legacy setup): - bind SLF4J with `slf4j-reload4j`, and the Log4j 1 calls are already native; - `log4j-over-slf4j` must go, and the guard described below enforces that at startup. A quick audit works for any backend: write the arrow for every logging jar the build resolves, confirm that exactly one arrow leaves SLF4J, and confirm that no two arrows run in opposite directions between the same pair. A framework starter may already bring some of these jars, so read the resolved dependency tree before adding or excluding anything; the exclusion syntax belongs to the build tool. ## The relocation of slf4j-log4j12 In 2.0.20 the `slf4j-log4j12` module is a `pom`-packaged artifact whose `distributionManagement` declares a **relocation** to `org.slf4j:slf4j-reload4j`. A build that still asks for `slf4j-log4j12` therefore resolves the reload4j provider. Treat both names as the same arrow, SLF4J into Log4j 1. ## The guard slf4j-reload4j raises `Reload4jLoggerFactory` has a static initializer that checks for the loop before any logging happens: 1. it calls `Class.forName("org.apache.log4j.Log4jLoggerFactory")`, a package-private class that exists in `log4j-over-slf4j`, not in the real Log4j 1; 2. if the class loads, it reports two lines through SLF4J's internal `Reporter.error`, the first being `Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. ` and the second pointing at `http://www.slf4j.org/codes.html#log4jDelegationLoop`; 3. it throws an `IllegalStateException` carrying the same text, which the JVM surfaces wrapped in an `ExceptionInInitializerError`. The message still says `slf4j-log4j12.jar` even though the class lives in `slf4j-reload4j`, which matters when you search logs or tickets. SLF4J's 2.0.20 source contains no comparable check for `log4j-to-slf4j` beside `log4j-slf4j2-impl`; both are Log4j 2 artifacts, so whatever they detect is their own behaviour, and a build rule is the dependable guard. ## What log4j-over-slf4j does and does not emulate `log4j-over-slf4j` is a minimal re-implementation of Log4j 1.2's classes, delegating every printing call to an `org.slf4j.Logger`: - `trace`, `debug`, `info`, `warn` and `error` map to their SLF4J equivalents; - `fatal` maps to SLF4J `error` with a marker named `FATAL`, because SLF4J has no FATAL level; - configuration calls such as `PropertyConfigurator.configure(...)`, `Category.addAppender(...)` and `Category.setLevel(...)` are **no-ops**. That last point is a frequent surprise in the scenario: an old library that configures its own Log4j 1 appenders or levels in code compiles and runs, but its configuration is silently ignored. Levels and outputs for its loggers must now be set in the bound backend.
- Why does a Log4j 1 fatal() call arrive in an SLF4J backend as ERROR?SLF4J has no FATAL level. `log4j-over-slf4j` maps `fatal()` to SLF4J's error level and attaches a marker obtained with `MarkerFactory.getMarker("FATAL")`. A backend that can filter or render markers can still tell those lines apart from ordinary errors; one that ignores markers shows them as plain ERROR.
- After switching an old library to log4j-over-slf4j, its custom Log4j 1 appender stopped writing its file. Why?`log4j-over-slf4j` keeps Log4j 1's classes only so calling code links. `PropertyConfigurator.configure(...)` is a no-op class, and `Category.addAppender(...)` and `Category.setLevel(...)` do nothing, so the library's configuration is silently dropped. Its events now flow into SLF4J, and any separate file or level for its loggers has to be configured in the bound backend.
- Why does the slf4j-reload4j guard's message name slf4j-log4j12.jar when the check runs inside slf4j-reload4j?The text was kept from the old Log4j 1.2 binding: `Reload4jLoggerFactory` still reports `Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.` Since `slf4j-log4j12` now only relocates to `slf4j-reload4j`, read either name as the same SLF4J-to-Log4j 1 arrow, and search logs and tickets for both.
Think of each jar as a one-way street sign between two towns. Signs pointing from every old town into SLF4J and one sign from SLF4J to your backend get all traffic home; a pair of signs pointing opposite ways between the same two towns sends cars round the same block until the road jams.
- log4j-over-slf4j and slf4j-reload4j do the same job and are interchangeable.
- log4j-to-slf4j is needed whenever Log4j 2 is the backend.
- Declaring slf4j-log4j12 in 2.0.20 still gets the original Log4j 1.2 binding.
- log4j-over-slf4j honours a library's PropertyConfigurator setup and appenders.
- SLF4J guards every bridge-and-provider loop at startup.
- The Log4j 2 API calls need log4j-over-slf4j to reach SLF4J.
Assumes SLF4J 2.0.20, where slf4j-log4j12 is a relocation pom to slf4j-reload4j and log4j-slf4j2-impl is the Log4j 2 provider artifact for the SLF4J 2.0 API line.