Logging in Java using Logback with Example

Logback is a widely used logging framework for Java applications. Logback was created to replace the popular log4j project by Ceki Gülcü, the founder of Log4j.

Logback is divided into three modules:

  • Logback Core - The logback-core module provides the core functionality and is also responsible for laying the foundation for the other two modules.
  • Logback Classic - The logback-classic module extends Logback's core functionality to provide support for logging libraries, such as SLF4J, Log4j. It natively implements the SLF4J API, allowing a developer to easily switch between logback and other logging frameworks such as java.util.logging or log4j 1.x.
  • Logback Access - The logback-access module helps to integrate with Servlet containers like Tomcat and Jetty to provide HTTP-access log functionality.

Logback is built upon Logger, Appender, and Layout components to enable developers to log messages based on message type and level, as well as manage how these log messages are formatted and reported at runtime.


A Logger component is part of the logback-classic module. The Logger component helps to log messages. Following is the basic template for logging:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Example {
    private static final Logger LOGGER = LoggerFactory.getLogger(Transaction.class);
	public void methodName(){
		LOGGER.info("calling methodName()....")


An Appender component is part of the logback-core module. The Appenders help to write log messages to different destinations. A logger can be configured with multiple appenders. Logback provides the following ready to use appenders:

  • OutputStreamAppender - It appends log events to a java.io.OutputStream.
  • ConsoleAppender - It appends log events on the console.
  • FileAppender - It is a subclass of OutputStreamAppender. It appends log events into a file.
  • RollingFileAppender - It is a subclass of FileAppender. It appends logs events into a file with the capability to rollover log files.
  • SocketAppender and SSLSocketAppender - The SocketAppender is designed to send logs to remote destinations. The SSLSocketAppender extends the capability of SocketAppender to send log messages over Secure Sockets Layer (SSL).
  • ServerSocketAppender and SSLServerSocketAppender - The ServerSocketAppender is designed to send log messages to remote destinations. When ServerSocketAppender is used, the appender acts as a server, actively listening on a TCP socket for inbound connections from interested clients. All connected clients receive logging events supplied to the appender. The SSLServerSocketAppender extends the capability of ServerSocketAppender to send log messages over Secure Sockets Layer (SSL).
  • SMTPAppender - SMTPAppender collects logging events in one or more fixed-size buffers and sends the contents of the relevant buffer in an email. The email is sent in an asynchronous manner. By default, email is triggered by a logging level of ERROR.
  • DBAppender - The DBAppender writes logging events to three database tables in a format that is not dependent on Java programming language. The three tables are logging_event_property, logging_event, and logging_event_exception. Before DBAppender can be used, the three tables must exist. Logback includes SQL scripts for creating the tables in the logback-classic/src/main/java/ch/qos/logback/classic/db/script directory. If a script for your specific sort of database system isn't available, it should be quite simple to create one based on existing scripts.
  • SyslogAppender - The SyslogAppender sends log events to a remote syslog receiver, also commonly called syslog daemon or syslog server.
  • AsyncAppender - The AsyncAppender appends log events asynchronously.
  • SiftingAppender - The SiftingAppender appends log events to a separate log file.


A Layout component is part of the logback-core module. Layouts help to transform an incoming event into a formatted string.

Logback Configuration

Logback can be configured programmatically or by a configuration script written in XML or Groovy.

Logback tries to configure itself by looking for a file named logback-test.xml in the classpath. If no such file is found, it looks for logback.xml file in the classpath. If Logback could not find any configurations, it uses the BasicConfigurator that directs the logging output to the console.

Logback Example

In this example, we will show you how to use Logback framework for logging in a Java application.

This example uses logback core, logback classic, and SLF4J. SLF4J stands for Simple Logging Facade for Java. It is a simple facade that provides abstraction for other logging frameworks.

Follow the steps below to complete this example:

Adding Logback Dependencies

For Maven

Add the following dependencies to pom.xml file:

For Gradle

Add the following dependencies to build.gradle file:

implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.3.0-alpha13'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha13'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha7'

NOTE: The versions of the above given Logback dependencies must be compatible with one another, else your application will fail to run.

Adding Logback Configuration File

Create a file named logback.xml inside the src/main/resources folder of your application and add the following configuration to it:

Logging Data

The following are example classes that shows how to log data:


package com.tutorialsbuddy.logging.example.transaction.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tutorialsbuddy.logging.example.transaction.TransactionService;

public class TransactionServiceImpl implements TransactionService {
	private static final Logger LOGGER = LoggerFactory.getLogger(TransactionServiceImpl.class);

	public void inititate(String senderId, String receiverId, double trxnAmount) {
		LOGGER.debug("calling initiating(senderId : {}, receiverId : {}, trxnAmount : {}) method...", senderId,
				receiverId, trxnAmount);
		try {
			int testResult = (int) trxnAmount / 0;
			LOGGER.info("Result = {}", testResult);
		} catch (Exception ex) {
			LOGGER.error("Exception caught while initiating transaction " + ex);

	public void refund(String transactionId, double refundAmount) {
		LOGGER.debug("calling initiating(transactionId : {}, refundAmount : {}) method...", transactionId,
		try {

		} catch (Exception ex) {
			LOGGER.warn("Exception caught while refunding transaction " + ex);


package com.tutorialsbuddy.logging.example.user.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tutorialsbuddy.logging.example.user.UserService;

public class UserServiceImpl implements UserService {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

	public void login(String username, String password) {
		LOGGER.info("calling login(username : {}) method...", username);

	public void logout(String username) {
		LOGGER.debug("calling logout(username : {}) method...", username);