Log4j2
High-performance logging with async appenders and structured output.
What it is
Log4j2 is a high-performance, flexible, and reliable logging framework for Java applications. It is the successor to Log4j and provides advanced features such as asynchronous logging, custom log levels, and plugin support.
Log4j2 allows developers to log messages at different levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL). It supports configuration through XML, JSON, YAML, or properties files and integrates with SLF4J for logging abstraction.
- Watch for
- Log4Shell (CVE-2021-44228) affected 2.x below 2.17 — keep it current, and never log untrusted input as a pattern
- Licence
- Apache 2.0
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- High-throughput services where logging overhead is measurable
- You need structured JSON logs for a centralised log platform
Look elsewhere when
- Spring Boot defaults to Logback, and there is rarely a reason to swap it
Installation
Add org.apache.logging.log4j:log4j-core and org.apache.logging.log4j:log4j-api dependencies in pom.xmlGetting started
The smallest useful thing you can do with it, and what each part means.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyApp {
private static final Logger logger = LogManager.getLogger(MyApp.class);
public static void main(String[] args) {
logger.info("Application started");
}
}String user = "Alice";
logger.debug("User {} has logged in", user);Advanced usage
Where the library earns its place over a simpler alternative.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
logger.error("An error occurred: {}", e.getMessage(), e);
}<Configuration status="WARN">
<Appenders>
<Async name="AsyncConsole">
<Console />
</Async>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="AsyncConsole" />
</Root>
</Loggers>
</Configuration><Logger name="com.example" level="debug" additivity="false">
<AppenderRef ref="FileAppender" />
</Logger>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger(MyApp.class);
logger.info("Message through SLF4J facade");Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- NoClassDefFoundError for LogManager or Logger
- Ensure both log4j-api and log4j-core are present in the classpath.
- StatusLogger warnings
- Check configuration file format and location; Log4j2 may fallback to default configuration if it cannot find the custom config.
Best practices
- Use asynchronous logging for high-throughput applications.
- Externalize configuration in XML, JSON, YAML, or properties files for flexibility.
- Use parameterized logging instead of string concatenation for performance.
- Organize loggers per package or class for clarity.
- Leverage multiple appenders (console, file, rolling file, etc.) for different logging needs.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Log4j2 was created by the Apache Software Foundation to address performance and flexibility limitations in Log4j 1.x. It leverages a plugin architecture, supports various appenders and layouts, and provides better performance through asynchronous logging and garbage-free design.
