Search This Blog

Spring Boot 2 Logging SLF4j Logback and LOG4j2 Example

In this article, we learn how to log effectively with Spring Boot. We will look at Spring Boot Starter for Logging. We will look at the defaults in Spring Boot for Logging - Logback, and SLF4J. We will also be looking at the Spring Boot starter for log4j2.

First, we will discuss a few important points about Spring Boot logging feature and then we will create a simple example to demonstrate the same.

Spring Boot 2 Logging Feature Overview

Logging is a very important part of any application and it helps with debugging issues. Spring Boot, by default, includes spring-boot-starter-logging as a transitive dependency for the spring-boot-starter module. By default, Spring Boot includes SLF4J along with Logback implementations.

If Logback is available, Spring Boot will choose it as the logging handler. You can easily configure logging levels within the application.properties file without having to create logging provider-specific configuration files such as logback.xml or log4j.properties.

Logger name: This is usually the source class name (often abbreviated).

The log message.

Console Output

The default log configuration echoes messages to the console as they are written. By default, ERROR-level,WARN-level, and INFO-level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.

$ java -jar myapp.jar --debug

You can also specify debug=true in your application.properties.

File Output

By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging.file or logging.path property (for example, in your application.properties).

So far we understood, how logging works in Spring Boot and default configurations. Now it's time to use Spring Boot 2 logging feature in our project so let's create an example to demonstrate how to use Spring Boot 2 logging feature.

Simple Spring Boot 2 logging Application

Let's develop a simple in-memory Spring Boot 2 logging application.

1. Creating and Importing a Project

There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.

Look at the above diagram, we have specified the following details:

Generate: Maven Project

Java Version: 1.8 (Default)

Spring Boot:2.0.4

Group: net.guides.springboot2

Artifact: springboot2-logging

Name: springboot2-logging

Package Name : net.guides.springboot2.springboot2-logging

Packaging: jar (This is the default value)

Dependencies: Web

Once, all the details are entered, click on Generate Project button will generate a spring boot project and downloads it. Next, Unzip the downloaded zip file and import it into your favorite IDE.

2. Packaging Structure

Once we will import generated spring boot project in IDE, we will see some auto-generated files. Refer above diagram for project structure.