Logging with SLF4J and Logback

Logging is so important in software development. In simple projects, JDK logging can be a solution, but not for enterprise applications because it lacks flexibility. This tutorial will help you know how to do logging with SLF4J and Logback.

Related Article: Logging with SLF4J

I. Introduction
1. SLF4J

There are many famous frameworks to be used for logging: Log4j, Logback, java.util.logging… If we wanna change the logging framework without affecting Java code, SLF4J is the good choice.

SLF4J, the Simple Logging Facade for Java, serves as a simple facade or abstraction for many logging frameworks above. The layer we apply logging is independent from logging implementation. We only have to choose the configuration of logging system.

For details, please read previous post: Logging with SLF4J.

2. Logback

Logback, designed by Log4j‘s founder, is intended as a successor to the popular Log4j project. It builds upon a decade of experience gained in designing industrial-strength logging systems. So, Logback is faster and has a smaller footprint than all existing logging systems, sometimes by a wide margin.

Logback improve many advantages over Log4j:
– faster implementation
– logback-classic speaks SLF4J natively
– configuration files in XML or Groovy
– automatic reloading of logging configuration
– better filter
– automatic compression of archived log files
– stack traces with packaging data (such as jar file)
– automatic removal of old log archives

Logback Architecture
Logback has three modules: logback-core, logback-classic and logback-access:
– core module lays the groundwork for the other two modules
– classic module extends core, it is likely improved version of log4j. Logback-classic implements SLF4J API so we can easily switch between Logback and other logging frameworks.
– access module integrates with Servlet containers to provide HTTP-access log functionality.

II. Overview
1. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Project Structure

logback-slf4j-structure

3. Step to do

– Create Maven project
– Add Dependencies & Plugins
– Create XML Configuration for Logback
– Create MainApplication Class
– Run Application & Enjoy Result

III. Practice
1. Create Maven project

– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.
– Check Create a simple project, choose Workspace Location and click Next.
– Fill all fields in Artifact group box, then click Finish.

2. Add Dependencies & Plugins

Open pom.xml, add:

3. Create XML Configuration for Logback

Under src/main/resources, add logback.xml file:

Logback delegates task of writing a logging event to appenders. This example uses 2 kinds of appender: ConsoleAppender and RollingFileAppender (inherit from FileAppender).
ConsoleAppender appends on the console (System.out or System.err) with the help of an encoder specified by Pattern.
FileAppender appends log events into a file. The target file is specified by the File option.
RollingFileAppender extends FileAppender can rollover log files. For example, it can log to a file named logback.log, then change its logging target to another file when meeting a certain condition. This example uses RollingFileAppender with rollingPolicy to rollover log file every minute with 10 minutes history keeping.

For details about Pattern, please visit Conversion Word.
In this example,

will create a result like this:

4. Create MainApplication Class

Under package com.javasampleapproach.logback, create MainApp.java:

Using SLF4J is easy, we get a logger, and log messages at some logging levels (TRACE, DEBUG, INFO, WARN, ERROR). Because we set level="DEBUG", so the logger only appends these logging levels: DEBUG, INFO, WARN, ERROR.

You can understand SLF4J deeply in previous article: Logging with SLF4J.

5. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:

– Open E:/tmpLog/logback.log:

– We can also see more rolling log files in E:/tmpLog/ folder:
logback-slf4j-folder

IV. Source Code

logbackslf4j

By JavaSampleApproach | December 6, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*