Logging for Java: Best Solutions (Log4j, SLF4J, Logback, java.util.logging)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Logging for Java: Best Solutions



Logging is an integral part of developing any serious Java application. It allows you to track errors, analyze system behavior, and collect metrics. In Java, there are several mature solutions, each with its own features. In this article, we will cover four main approaches: Log4j 2, SLF4J (as a facade), Logback, and the built-in java.util.logging.



1. java.util.logging (JUL) — Built-in Solution

JUL comes bundled with the JDK, so it requires no additional dependencies. However, its functionality is limited compared to third-party libraries. Configuration is typically done via the logging.properties file.



Configuration and usage example:

// logging.propertieshandlers = java.util.logging.ConsoleHandler.level = INFOjava.util.logging.ConsoleHandler.level = FINEST


import java.util.logging.Logger;

public class JulExample { private static final Logger logger = Logger.getLogger(JulExample.class.getName());

public static void main(String[] args) { logger.info("This is an informational message"); logger.warning("Warning!"); logger.fine("Detailed debug information (FINEST level)"); }}

Pros: zero dependencies, simplicity.

Cons: limited flexibility, lower performance, difficulty with fine-tuning.



2. Log4j 2 — Powerful and Modern Solution

Log4j 2 is the successor to the popular Log4j 1.x. It offers asynchronous logging, JSON support, filters, and a wide range of appenders (files, databases, Kafka).



Dependency (Maven):

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


Configuration example (log4j2.xml):

<?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN">    <Appenders>        <Console name="Console" target="SYSTEM_OUT">            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>        </Console>    </Appenders>    <Loggers>        <Root level="info">            <AppenderRef ref="Console"/>        </Root>    </Loggers></Configuration>


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

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

public static void main(String[] args) { logger.debug("Debug: entering main method"); logger.info("Application started"); try { int result = 10 / 0; } catch (ArithmeticException e) { logger.error("Division by zero error", e); } }}

Pros: high performance (asynchronous appenders), flexible configuration, multithreading support.

Cons: need to manually add dependencies, configuration complexity for beginners.



3. Logback — Successor to Log4j 1.x

Logback is considered the direct successor to Log4j and is developed by the same author. It is faster, has built-in SLF4J support, and automatic configuration reloading.



Dependency (Maven):

<dependency>    <groupId>ch.qos.logback</groupId>    <artifactId>logback-classic</artifactId>    <version>1.4.14</version></dependency>


Configuration example (logback.xml):

<                    

Recommendations