Benchmarking Java Logging Frameworks Without Guesswork
Logging is often treated as a harmless diagnostic feature until a Java service reaches production traffic. A single request may produce several log events, and each event can involve message construction, formatting, timestamp handling, thread coordination, queueing, and I/O. The cost becomes visible in CPU usage, allocation rate, latency, and the amount of data sent to an observability platform.
Logback and Log4j 2 are both mature choices for Java applications, usually accessed through the SLF4J API. Their relative performance depends less on the logger name than on the configuration around it: synchronous versus asynchronous appenders, plain text versus JSON, enabled versus disabled levels, and the destination receiving the events. A useful benchmark therefore measures realistic logging paths rather than declaring one framework universally faster.
What The Benchmark Should Measure
A logging benchmark needs to separate the cost of deciding to log from the cost of actually writing a record. Disabled debug logging is mainly a test of level checks and argument handling. Enabled console logging measures formatting and interaction with a stream, while file logging adds buffering and operating-system effects. An asynchronous configuration measures how quickly the application can hand work to a queue, rather than how quickly the final destination persists it.
A practical benchmark should include at least four scenarios:
- A disabled
DEBUGstatement - An enabled parameterised message
- A structured JSON event with several fields
- A high-volume asynchronous appender
The test should use both constant strings and dynamic values. Code such as logger.debug("User {}", userId) can avoid some unnecessary string construction, while logger.debug("User " + userId) performs concatenation before SLF4J checks whether debug logging is enabled. If the application uses MDC, exceptions, markers, or correlation IDs, those features belong in the test as well.
The destination must be controlled carefully. A console appender connected to a terminal is unsuitable for repeatable throughput testing, and a file on a busy developer laptop can produce inconsistent results. A temporary local file, a memory-backed sink, or a controlled discard appender can isolate framework overhead. The benchmark should still include a separate end-to-end test that accounts for real disk or network output.
A Reproducible JMH Design
JMH is the natural starting point for microbenchmarks on the JVM because it handles warm-up, forks, compiler optimisation, and measurement windows. A typical setup might use JDK 21, SLF4J 2.0, Logback 1.5, and Log4j 2.23 or a current equivalent. Exact versions should be recorded because logging implementations change their allocation behaviour and asynchronous internals over time.
Each benchmark method should run in multiple forks with several warm-up and measurement iterations. Throughput alone is insufficient: include average time, allocation rate, and selected percentile latency where the benchmark harness supports it. A test that produces impressive events per second while allocating heavily may increase garbage-collection pauses under application load.
The workload should be generated outside the timed operation where possible. For example, pre-create representative user IDs and request paths, then consume results with a JMH Blackhole when logging is disabled. Otherwise, the JIT compiler may remove work that appears unrelated to the benchmark. For asynchronous logging, wait for the queue to drain after each run and record queue depth or dropped-event behaviour.
Run the same test on a quiet machine and repeat it in a container with realistic CPU limits. A modern laptop in Sydney can produce very different results from a two-vCPU Kubernetes pod in an AWS Sydney region. CPU frequency scaling, container throttling, filesystem settings, and the selected garbage collector all influence the outcome.
Representative Results And Their Limits
The following figures are illustrative results from a controlled JDK 21 benchmark rather than universal promises. The workload logs a short parameterised message, then a JSON event containing six fields. The synchronous tests write to a buffered local sink; the asynchronous tests measure the caller’s hand-off path while ensuring that the queue is drained before the next iteration.
In this kind of workload, disabled logging usually sits in the low single-digit nanosecond range once the level check is optimised. The difference between Logback and Log4j 2 is often smaller than the cost of an accidental string concatenation. Enabled plain-text logging commonly lands in the hundreds of nanoseconds per event with a memory sink, while JSON encoding can raise that substantially through escaping, field handling, and temporary objects.
A representative pattern might show Log4j 2 slightly ahead for high-volume asynchronous logging, with Logback close behind for synchronous text output. Such a result should not be read as a permanent ranking. Appender choices, queue implementation, layout configuration, stack-trace rendering, and whether garbage-free features are enabled can reverse the result.
The important benchmark observation is the gap between a caller returning and a log record being safely written. Asynchronous logging can reduce request-thread latency while moving pressure to a background worker and queue. If the queue fills, the application may block, discard events, or apply a configured policy. A benchmark that measures only caller throughput hides this operational trade-off.
Where Logging Costs Appear
Message creation is often the first avoidable cost. Parameterised SLF4J calls are preferable to eager concatenation, especially for disabled levels. However, a parameterised call still incurs work when the level is enabled, and a large object’s toString() method can be expensive. Guarding a complicated computation with isDebugEnabled() may be worthwhile, while guarding every small scalar substitution can make application code noisy.
Layouts have a major effect. Plain text with a timestamp and thread name is comparatively inexpensive. JSON logging adds predictable structure for systems such as OpenSearch, Splunk, or a managed cloud logging service, but it requires escaping and serialisation. Stack traces are particularly costly because they involve formatting many lines and can create large payloads. Logging an exception once at a boundary is usually cheaper than repeatedly logging it through several layers.
Synchronous appenders provide straightforward delivery semantics: when the logging call returns, the appender has completed its configured work. That simplicity can expose disk latency or a slow collector directly to request threads. Async appenders reduce that coupling, but queue capacity, worker throughput, and shutdown flushing become part of the design. A queue that is too small creates bursts of blocking or loss; one that is too large can retain substantial memory during an incident.
For Australian services, network placement can affect this decision. A team running workloads in AWS Sydney or Azure Australia East may send logs to a local regional endpoint, while a SaaS observability service may process data elsewhere. The benchmark should model the intended route and account for egress, ingestion limits, and any retention policy required by the business.
Reading Results In A Production Context
A benchmark should be compared with application traffic, not with an abstract record count. If a service handles 2,000 requests per second and emits eight events per request, it produces 16,000 events per second before retries, access logs, and error bursts. A framework difference of 100 nanoseconds per event is then much less important than a JSON payload that is several kilobytes or a collector that intermittently stalls.
Measure CPU consumption and allocation while applying realistic concurrency. A single-thread benchmark can favour a simple synchronous path, whereas a web service with dozens of request threads may benefit from asynchronous hand-off. Test burst behaviour as well as steady state: deployments, payment failures, and upstream outages often produce the highest logging volume.
Correctness belongs in the benchmark too. Verify that fields are not lost, event order is acceptable, exceptions retain their causes, and shutdown flushes pending records. An asynchronous logger that is marginally faster but loses diagnostic events during pod termination may be unsuitable for a critical service. Check queue saturation and the chosen error policy explicitly.
Australian organisations also need to consider personal information. Request bodies, email addresses, account identifiers, and access tokens should not enter logs merely because a JSON encoder makes it easy. The Privacy Act and internal data-retention rules can make the cost of storing and securing excessive logs greater than the CPU cost measured by JMH. Benchmark the redaction and filtering rules that will actually run in production.
Choosing Between Logback And Log4j 2
Logback remains a practical default for many Spring and general JVM applications. It integrates naturally with SLF4J, has familiar configuration, and offers solid synchronous and asynchronous options. Teams that value straightforward operation and already use Logback may gain little from changing frameworks purely for a small microbenchmark advantage.
Log4j 2 provides a broad asynchronous logging model and configuration features that suit high-throughput services. Its garbage-free options and asynchronous components can reduce allocation in carefully configured workloads, although they require testing rather than assuming a setting is beneficial. The additional configuration flexibility can also increase operational complexity.
Security and maintenance should be part of the selection. Keep either framework current, monitor advisories, and avoid copying old configuration examples without checking the supported version. Dependency convergence matters when libraries bring their own SLF4J bindings or bridges. A clean dependency graph is easier to upgrade and less likely to produce duplicate events.
The choice may differ between a Melbourne fintech backend, a Brisbane e-commerce platform, and a small consultancy application. High-volume structured logs and strict latency targets may justify a deeper Log4j 2 evaluation. A smaller service with moderate traffic may reasonably prefer the simpler configuration already standardised across its codebase.
| Scenario | Logback | Log4j 2 | Main Benchmark Concern |
|---|---|---|---|
| Disabled debug statements | Very low overhead with correct parameterisation | Very low overhead with correct parameterisation | Accidental string construction |
| Synchronous text logging | Strong, predictable baseline | Strong, predictable baseline | Layout and destination latency |
| Structured JSON logging | Effective with a suitable encoder | Effective with a suitable layout or template | Allocation and escaping |
| Asynchronous high-volume logging | Good with tuned async appenders | Often attractive with async and garbage-free options | Queue saturation and delivery policy |
| Existing SLF4J application | Simple migration and familiar setup | Straightforward binding change, with configuration review | Dependency and bridge conflicts |
| Operational priority | Simplicity and broad familiarity | Flexibility and high-throughput tuning | Maintenance, upgrades, and incident behaviour |
The result to remember is that logging performance is a system property, not a brand comparison. Benchmark the exact JDK, framework versions, layouts, appenders, concurrency, and destination your service will use. In most applications, disciplined message construction, sensible log volume, bounded asynchronous queues, and careful handling of sensitive data matter more than a small difference between Logback and Log4j 2.