Java logging libraries: choose an API, backend, and bridge
Compare seven Java logging options, follow the runtime classpath, and see how Logback and Log4j 2 encode the same SLF4J event.
After a dependency upgrade, a checkout error still reaches stdout, but the alert no longer matches it. The new encoder moved the severity field, and another dependency has supplied a second logging provider. Those are two separate problems. First establish which runtime implementation owns the output. Then check what it writes and what your collector actually stores.
Separate the API from the code that writes the log
| Option | Role in a Java application | What changes at runtime |
|---|---|---|
| SLF4J | Logging facade | SLF4J 2 discovers a compatible provider. With none, it warns and falls back to a no-op logger. |
| Logback | Backend through Logback Classic | Appenders control where records go. An encoder such as the built-in JsonEncoder controls their format. |
| Log4j 2 | Separate API and Log4j Core backend | Core provides appenders and async loggers. JSON Template Layout comes from a separate module. |
| java.util.logging (JUL) | API and logging implementation in the JDK | Handlers and formatters can write to the console or a file with no external backend. |
| Apache Commons Logging (JCL) | Facade commonly encountered in dependencies | Still maintained. Check the implementation or bridge loaded in your process. |
| tinylog 2 | API and implementation | With both artifacts on the classpath it starts with console output. A writer can target a rolling file or JSON file. |
| Flogger | Fluent API with backend options | The standalone setup uses a separate runtime backend. |
If you publish a library, using SLF4J as an API leaves the provider decision to the application. For a Spring Boot service already using the default starters, Logback is a reasonable starting point. Log4j Core earns the change when you need something concrete, such as JSON templates or the Disruptor-backed async logger. A small JDK-only utility can use JUL directly. tinylog's static API and writers suit a utility that needs a different output format, while Flogger is a separate API choice with call-site rate controls. SLF4J manual · Spring Boot logging · tinylog configuration · Flogger
Follow the arrows on the runtime classpath
Look at mvn dependency:tree -Dscope=runtime and confirm which artifacts actually made it into the packaged application. SLF4J 2 providers differ from the old 1.7 bindings. For one classpath, pick one compatible provider. A duplicate line can also come from an appender attached to both a logger and its ancestor, so a second line alone doesn't diagnose a second provider.
The direction matters. log4j-slf4j2-impl sends SLF4J calls to Log4j 2. log4j-to-slf4j sends Log4j API calls the other way. Putting both in the same runtime can create a loop. The same principle applies to jul-to-slf4j with an installed bridge handler and the reverse provider slf4j-jdk14. Spring Framework 5 and 6 have their own Commons Logging bridge, spring-jcl, which can route based on the available API and fall back to JUL. Check what is already present before adding another adapter. Log4j installation · SLF4J bridges · Spring Framework 6.2 logging · JCL releases
For Log4j 1.x, the compatibility module log4j-1.2-api can bridge older calls while you migrate. Inventory old configuration and custom appenders separately. The bridge doesn't guarantee that they behave the same on Log4j 2. Apache migration guide
Keep the fields your queries already rely on
This small program logs one synthetic error with a request ID, clears the context, and emits another record. Run it with one SLF4J 2 provider at a time and configure that backend for JSON.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public final class LoggingSample {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(LoggingSample.class);
MDC.put("request_id", "fixture-42");
try {
logger.error("checkout failed",
new IllegalStateException("synthetic failure"));
} finally {
MDC.remove("request_id");
}
logger.info("fixture complete");
}
}
In a local Java 21 check with SLF4J 2.0.20, Logback 1.6.6 with JsonEncoder wrote level and mdc.request_id. Log4j 2.26.1 with the default JSON Template Layout wrote literal keys log.level and request_id. The timestamp and exception representations also differed. Both second records omitted the request ID. This check exercises formatting and context cleanup with two configurations. It gives no throughput estimate.
In a service, distinguish the application's request ID from an actual trace ID. When moving from a file to container stdout, or from one JSON layout to another, check the timestamp, severity, message, exception and correlation fields that arrive downstream. With DataKit collecting file or container output, adjust parsing or field mapping before expecting existing filters and alerts to match. For Spring Boot versions with built-in structured logging, logging.structured.format.console can select a JSON format. A custom Logback encoder or Log4j layout still needs to support the selected format. DataKit log collection · Logback encoders · Log4j JSON Template Layout
Measure the output path before adding a queue
A queue can absorb a burst, but it can't make a slow destination keep up indefinitely. Measure request latency and output throughput with the real appender and layout. Check what happens at capacity, including whether events are dropped, producers block, or some calls run synchronously. Pending events can disappear on an abrupt exit. For audit records, verify the durability and integrity of the full path. A synchronous appender by itself doesn't establish either. Log4j async logger trade-offs · Logback AsyncAppender
Configure Logback · Configure Log4j 2 · Java logging practices
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan
