Comprehensive Guide to Log Configuration and Performance Comparison of Major Java Logging Frameworks
The article explains why logging is essential for problem tracing, status monitoring, and security auditing, introduces Log4j, Log4j2, and Logback components and configuration files, demonstrates usage with Maven dependencies and code snippets, evaluates their synchronous and asynchronous performance, and offers practical recommendations for production environments.
1. Background
Logging is a ubiquitous requirement in any commercial software, serving three main purposes: problem tracing, status monitoring, and security auditing.
2. Log4j
2.1 Introduction
Log4j, created by Ceki Gülcü and contributed to the Apache Software Foundation, consists of three core components: Loggers, Appenders, and Layouts. These allow developers to define the log category, destination, and format.
The logger hierarchy defines five levels (DEBUG < INFO < WARN < ERROR < FATAL). Only messages with a level equal to or higher than the configured level are emitted.
2.2 Project Integration
Add the following Maven dependencies to pom.xml:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>Create a log4j.xml (or log4j.properties) file in the project root to configure loggers, appenders (console, file, socket, jdbc) and layouts. Example XML configuration is provided in the source.
Typical usage in code:
package org.example.log4j.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogPrintUtil {
private static final Logger logger = LoggerFactory.getLogger(LogPrintUtil.class);
public static void main(String[] args) {
logger.info("info信息");
logger.warn("warn信息");
logger.error("error信息");
}
}Calling logger.isInfoEnabled() before constructing log messages can avoid unnecessary string concatenation when the current log level is higher than INFO, improving performance especially under high concurrency.
3. Log4j2
3.1 Introduction
Log4j2 is the successor of Log4j 1.x, incorporating design ideas from Logback and fixing several issues, resulting in noticeable performance gains.
Exception handling improvements.
Significant performance boost over Log4j 1 and Logback (official benchmark data referenced).
Automatic configuration reload, enabling dynamic log‑level changes without application restart.
Garbage‑free design to reduce JVM GC pressure.
3.2 Project Integration
Add Maven dependencies for Log4j2, SLF4J bridge, and Disruptor (required for asynchronous logging):
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>Create log4j2.xml in the project root. The configuration defines properties for log directories, console and rolling file appenders, threshold filters for INFO, WARN, and ERROR levels, and both synchronous and asynchronous root loggers. Example snippets are included in the source.
Usage is identical to Log4j 1: obtain a logger via LoggerFactory.getLogger(...) and call info, warn, error as needed.
4. Logback
4.1 Introduction
Logback, also authored by the creator of Log4j, offers three modules: logback-core, logback-classic (SLF4J‑compatible), and logback-access. It generally provides better performance than Log4j 1.
4.2 Project Integration
Add Maven dependency:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>2.7.8</version>
</dependency>Logback searches for configuration files in the following order: system property logback.configurationFile, logback.groovy on the classpath, logback-test.xml, then logback.xml. The first found file is used. The configuration can define console and file appenders, pattern layouts, rolling policies, and level filters. An example XML configuration is provided.
Typical usage mirrors Log4j2:
package org.example.logback.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogPrintUtil {
private static final Logger logger = LoggerFactory.getLogger(LogPrintUtil.class);
public static void main(String[] args) {
logger.info("info信息");
logger.warn("warn信息");
logger.error("error信息");
}
}5. SLF4J Bridge
SLF4J is a façade that does not perform logging itself; it delegates to the underlying logging implementation (Log4j, Logback, or Log4j2) via bridges. By changing only the Maven dependencies, a project can switch its logging backend without modifying source code that uses LoggerFactory.getLogger(...).
6. Performance Comparison of the Three Main Frameworks
The author measured the time to log 10,000 messages for Log4j 1, Log4j2, and Logback under both synchronous and asynchronous configurations on a Windows 7 machine. Results (illustrated with two images) show:
Console output should be avoided in production.
For pure file output, Logback outperforms Log4j2, and Log4j2 outperforms Log4j.
In production, Logback is recommended for synchronous file logging; if Log4j2 is preferred, use its asynchronous mode for near‑real‑time output.
Because logging involves disk I/O and, in Log4j 1, synchronized locks to guarantee thread safety, excessive logging can become a performance bottleneck. Therefore, log messages should be concise and unnecessary logs avoided.
7. Conclusion
The article presented a detailed overview of the three widely used Java logging frameworks, their configuration methods, code integration, and performance characteristics, helping developers choose the appropriate solution for their projects.
8. References
1. Log4j2 official website<br/>2. 廖雪峰 Java tutorial (original Chinese source)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
