Log4j, Log4Java
Log4j (Log for Java) is a widely used Java-based logging framework developed by the Apache Software Foundation. It allows developers to record application events (logs) in a structured, configurable, and flexible way.
With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.
There are three ways to configure log4j: with a properties file, with an XML file and through Java code.
Why Log4j is used?
Log4j is used to:
• Track application behavior and flow
• Debug issues in development and production
• Record errors and exceptions
• Monitor system health
• Support auditing and compliance
• Provide insights for performance tuning
How Log4j works?
Log4j follows a simple pipeline:
• Application generates a log event
• Log4j processes the event based on configuration
• Event is sent to an output destination (appender)
Log4j Log Levels
The following list defines the log levels and messages in log4j, in decreasing order of severity.
• OFF: The highest possible rank and is intended to turn off logging.
• FATAL: Severe errors that cause premature termination. Expect these to be immediately visible on a status console.
• ERROR: Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
• WARN: Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
• INFO: Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
• DEBUG: Detailed information on the flow through the system. Expect these to be written to logs only.
• TRACE: Most detailed information. Expect these to be written to logs only.
Key Features of Log4j
1. Multiple Output Destinations (Appenders)
Logs can be sent to:
• Console
• Files
• Databases
• Remote servers
• Syslog
2. Flexible Configuration
Can be configured using:
• XML
• JSON
• Properties files
No need to change code to change logging behavior.
3. Pattern Layouts
Allows customization of log format:
• timestamps
• thread info
• class names
• message structure
4. Performance Optimization
Supports asynchronous logging for better performance in high-load systems.
5. Hierarchical Loggers
Loggers follow package hierarchy, allowing fine-grained control.
Advantages of Log4j
1. Powerful and flexible
Highly configurable for different environments (dev/test/prod).
2. Better than System.out
Supports levels, formatting, and structured logs.
3. Multiple outputs
Can log to files, consoles, or external systems.
4. Performance-friendly
Asynchronous logging improves scalability.
5. Widely used
Industry-standard in Java enterprise applications.
Disadvantages of Log4j
1. Security risks (historical issue)
Log4j 2 had a major vulnerability (Log4Shell in 2021), which made systems vulnerable if not patched.
2. Configuration complexity
Advanced setups can be difficult for beginners.
3. Overhead if misused
Improper logging (too much DEBUG/TRACE in production) can impact performance.
4. Dependency management
Requires careful version updates to avoid security issues.
When to use Log4j?
Use Log4j when:
• Building Java applications (especially enterprise systems)
• You need structured and configurable logging
• You require production-grade logging with rotation and persistence
• You are working in microservices or distributed systems
Avoid or reconsider when:
• You are using non-Java ecosystems (use platform-native logging instead)
• You need extremely lightweight logging for small scripts
• You prefer simpler logging frameworks (like SLF4J + backend)
Alternatives to Log4j
Logback
Modern successor to Log4j 1, widely used
Java Util Logging
Built into Java standard library
Log4j 2
Improved version of Log4j with better performance and security
How to Use Log4j (Basic Example)
1. Add dependency (Maven)
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.x.x</version>
</dependency>
2. Create a Logger in Java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class App {
private static final Logger logger = LogManager.getLogger(App.class);
public static void main(String[] args) {
logger.info("Application started");
logger.debug("Debugging message");
logger.error("An error occurred");
}
}
3. Basic configuration (log4j2.xml)
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d [%t] %-5level %c - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>