Featured Article

Master Log4j Tutorial Java: Ultimate Guide for Developers

Kenneth Jul 13, 2026

In the vast landscape of Java programming, logging is a fundamental aspect that enables developers to monitor and debug their applications. Log4j, a popular logging library, simplifies this process by providing a flexible, extensible logging facility. If you're new to Log4j or seeking a comprehensive tutorial, you're in the right place. Let's delve into the world of Log4j and explore its features with practical examples.

How to log messages to File and Console in Java using Log4j? Example Tutorial
How to log messages to File and Console in Java using Log4j? Example Tutorial

Java provides a built-in logging package, java.util.logging, but it lacks the flexibility and configurability that Log4j offers. Log4j, developed by Ceki Güountedü, supports multiple output destinations (console, file, etc.), custom log levels, and more. It also plays well with other libraries, making it a popular choice among Java developers.

What is Log4j 2 RCE issue CVE-2021-44228? How to solve Log injection? [Tactical and Permanent Fix]
What is Log4j 2 RCE issue CVE-2021-44228? How to solve Log injection? [Tactical and Permanent Fix]

Getting Started with Log4j

Before we dive into Log4j's features, let's set up a simple project to ensure everything is working as expected. Include the Log4j library in your project's classpath. If you're using Maven, add this to your pom.xml:

Why use SLF4J over Log4J for logging in Java? Example
Why use SLF4J over Log4J for logging in Java? Example

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.14</version>
</dependency>

Configuring Log4j

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Log4j uses a properties file named log4j2.properties (or an XML file named log4j2.xml) for configuration. Place this file in your classpath's root. Here's a basic configuration:

# Set root logger level to INFO
rootLogger.level = info
rootLogger.appenderRef.stdout = true

# Define an output stream
appender.stdout.type = Console
appender.stdout.name = STDOUT
appender.stdout.layout.type = PatternLayout
appender.stdout.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger - %msg%n

Logging Levels

Is Your Java Logging Outdated? Why use SLF4J over Log4J?
Is Your Java Logging Outdated? Why use SLF4J over Log4J?

Log4j supports several logging levels, providing flexibility to control the flow of information. From highest to lowest severity, these are:

  • FATAL - Errors that prevent the application from continuing.
  • ERROR - Exceptional occurrences that should be logged.
  • WARN - Warnings from the system (log before errors).
  • INFO - Information messages (normal flow of application).
  • DEBUG - Detailed debugging information.
  • TRACE - Fine-grained debugging information.

Using Log4j in a Java Application

Why its better to use SLF4j over Log4j for logging in Java?
Why its better to use SLF4j over Log4j for logging in Java?

Now that we've configured Log4j let's use it to log messages in a Java application. Import the necessary classes:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Why use Log4j logging vs System.out.println in Java? Answer
Why use Log4j logging vs System.out.println in Java? Answer
10 Best Websites to Learn Java for Free in 2026
10 Best Websites to Learn Java for Free in 2026
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Java Patterns Notes 1
Java Patterns Notes 1
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Java Learning Roadmap 2026 ☕🚀
Java Learning Roadmap 2026 ☕🚀
the quick and easy way to learn how to use loops
the quick and easy way to learn how to use loops
java array operations
java array operations

Logging Instances

Create a Logger instance for your application's logger:

public class LoggerExample {
  private static final Logger logger = LogManager.getLogger(LoggerExample.class);

  public static void main(String[] args) {
    // Log messages here...
  }
}

Logging Messages

Use logger methods to log messages at different levels:

public class LoggerExample {
  // ...

  public static void main(String[] args) {
    logger.trace("This is a trace message");
    logger.debug("This is a debug message");
    logger.info("This is an info message");
    logger.warn("This is a warning message");
    logger.error("This is an error message");
    logger.fatal("This is a fatal message");
  }
}

Log4j Annotations

Log4j provides annotations to simplify logging. With the help of Log4j's Paul Baker, you can use these annotations to eliminate boilerplate code:

Using @Trace and Other Annotations

Add the following dependency to your project:

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api-jdk15</artifactId>
  <version>2.14</version>
</dependency>

Now, annotate your methods with @Trace, @Debug, @Info, @Warn, @Error, or @Fault:

public class LoggerAnnoExample {
  @Trace
  public void traceInfo() {
    // ...
  }

  @Debug("This is a debug message")
  public void debugInfo() {
    // ...
  }

  // More methods...
}

As your application's development continues, you'll find Log4j invaluable for monitoring and debugging. Learning Log4j's features and best practices will allow you to write more maintainable, resilient code.

Now that you're familiar with Log4j, consider exploring more advanced topics such as custom loggers, appenders, and layouts. Happy coding, and may your log files stay empty (that is, no errors!)! Remember, Log4j isn't just for debugging; it's a powerful tool for tracking and understanding your application's flow and behavior. So, dive deeper, experiment, and become a Log4j master!