Java Logging with Logback Framework

Logging Overview

Logging allows developers to track the execution flow and details of a running application.

Limitations of Simple Output Statements

  • Output is only visible in the console
  • Cannot be redirected to other destinations (files, databases)
  • Modifications to source code are required to disable output

Advantages of Logging Frameworks

  • Selective recording of system information to various destinations (console, files, databases)
  • Ability to control logging via configurable switches without code modification

Logback Framework

Logback is an open-source logging framework designed by the founder of log4j, offering improved performance over its predecessor.

Official website: https://logback.qos.ch/index.html

Logback implements the SLF4J (Simple Logging Facade for Java) logging specification.

Logback Architecture Components

Logback consists of three main modules:

  • logback-core: Provides the foundation for the other modules and is required
  • logback-classic: An enhanced version of log4j that fully implements the SLF4J API
  • logback-access: Integrates with servlet containers like Tomcat and Jetty to provide HTTP access logging

Required Dependencies

To use Logback, you need the following modules:

  • slf4j-api: The logging specification interface
  • logback-core: The core foundation module
  • logback-classic: The implementation module that extends the core

Logback Quick Start

Setup Process

  1. Create a lib folder in your project and add Logback JAR files, then add them to your project's libraries
  2. Place the core configuration file logback.xml in the src directory
  3. Obtain a logger instance in your code

Configuration File Example

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Console output configuration -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <encoder>
            <!-- Log output format:
                %d{yyyy-MM-dd HH:mm:ss.SSS} - Date and time
                %-5level - Log level (debug, info, error, etc.)
                %c - Class name where log is called
                %thread - Thread name
                %msg - Log message
                %n - Line break
            -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}[%-5level] %c [%thread] :%msg%n</pattern>
        </encoder>
    </appender>

    <!-- File output configuration -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <!-- Log file content format -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}-%msg%n</pattern>
            <!-- Log file encoding -->
            <charset>utf-8</charset>
        </encoder>
        <!-- Log file output path -->
        <file>./logs/application.log</file>
        <!-- Log file splitting and compression rules -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- Compression file naming pattern -->
            <fileNamePattern>./logs/application-%d{yyyy-MMdd}.log.zip</fileNamePattern>
            <!-- Maximum file size before splitting -->
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
    </appender>

    <!-- 
        Log level configuration:
        TRACE < DEBUG < INFO < WARN < ERROR
        The root logger can include zero or more appender-ref elements
    -->
    <root level="ALL">
        <appender-ref ref="CONSOLE"/> <!-- Console output -->
        <appender-ref ref="FILE"/>    <!-- File output -->
    </root>
</configuration>

Logger Initialization

In your Java code, obtain a logger instance:

public static final Logger LOGGER = LoggerFactory.getLogger("YourClassName");

Logback Configuration Details

Output Configuration

Logback's features are controlled through the logback.xml configuration file:

  • Use the <appender> tag to define output locations and formats
  • Common output destinations include console and files

Console Output Configuration

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    ...
</appender>

File Output Configuration

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    ...
</appender>

Finally, associate these appenders with your root logger:

<root level="ALL">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="FILE"/>    
</root>

Log Level Configuration

To control which log messages are displayed or disabled in production:

  • Set the log level to filter output
  • Log levels: TRACE < DEBUG < INFO < WARN < ERROR
  • Only messages with levels greater than or equal to the configured level will be displayed
  • Use ALL to enable all logging or OFF to disable all logging
  • Set the level in the <root level="INFO"> tag

Tags: java logback SLF4J logging Framework

Posted on Sat, 10 Oct 2026 16:12:14 +0000 by akano