Understanding logging in Spring Boot

Divya Rao
Javarevisited
Published in
4 min readJul 25, 2022

Introduction to SLF4J , loggers , log levels and formatting logs in Spring Boot

As a software developer, do you spend most of your time debugging your code? Going through the entire codebase and still not able to figure out where your code went awry? Here’s where logging comes to the rescue!

Logging is the process of tracking all the events that happen after a piece of code is run. It is a very important aspect of software development as it helps to track where exactly the code crashes and thus eases debugging .

Let’s understand how logging is done in spring boot!

To make logging easier for programmers, Java provides a variety of logging frameworks like : log4J, java.util.logging (JUL), tiny log, logback, etc.

A logging framework can be used to perform all the tasks like setting log file destinations, customizing log messages , etc.

Spring Boot comes with SLF4J inbuilt, which is an abstraction of all these logging frameworks. SLF4J stands for Simple Logging Façade for Java. It allows users to work with any of the logging frameworks with a single dependency.

Every logging framework comes with three elements.

  1. Logger — capture the messages
  2. Formatter — formats the messages captured by the logger
  3. Handler — Dispatches the messages by printing them on the console , or storing them in a file , sending an email, etc.

Let’s go step by step to understand each of these elements.

LOGGER

To generate loggers , we use the LoggerFactory class of the org.slf4j and the Logger interface is the entry point to slf4j API. The getLogger is a method of Logger Factory class that takes a string value as a name and provides the logger object of the specified name .

Logger logger = LoggerFactory.getLogger(Controller.class);

The Logger has methods that prints log messages for various log levels

Example :

logger.info("INFO MESSAGE")

What are log levels:

The messages logged can be of various security levels . Spring Boot supports five log levels which are

  1. ERROR — runtime errors
  2. DEBUG — Information about the flow of the system
  3. TRACE — more detailed information about the flow of the system
  4. WARNING — warning for the errors caused due to the usage of deprecated APIs.
  5. INFO — events occurring at the run time

Let’s write some code to create a logger . Initialize a spring project with Spring Initializer with Spring web dependency and create a basic rest controller

Controller.java

By default , the log level is set to INFO in Spring Boot. We can set log levels according to our requirements in the applications.properties file

The format to set the log level configuration is logging.level.[classpath] = [level].

logging.level.com.logging = TRACE

We can also set different log levels for different classes.

logging.level.root=INFO 
logging.level.com.logging =DEBUG

From this configuration, every class except the logging class path will have their log levels set to INFO

With the above discussion , this should be our updated code :

application.properties

By this , we can achieve the logs in the console.

Console output

Log format

Let’s break the log message and understand what each term means ,

  • 2022–07–16 13:16:51.810 — Date and Time
  • INFO — Log Level
  • 12620 — Process ID
  • [nio-8080-exec-1] — Thread name
  • com.logging.Controller — Logger name
  • Initializing Spring embedded WebApplicationContext — Log message

FORMATTER

The log messages can be formatted and customized according to our requirements by setting colors , message format , etc.

Console log output

In order to customize the messages in console , we can add few lines of code in the application.properties file

To enable colors ,

spring.output.ansi.enabled=always

To customize messages ,

logging.pattern.console= %d [%level] %c{1.} [%t] %m%n

where ,

%d — date

% level — log level

%c — class path

%t — thread executing

%m — message

%n — new line

We can add more customizations by mentioning the colors and date format by writing ,

logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){green} %clr([%level]){magenta}  %c{1.} [%t] %m%n

where,

%clr — color

We get the customized output and our console looks like this ,

console log output

HANDLER

We can dispatch the logs either by displaying the log message in the console as shown above , or else we can store the log messages in a file.

File log output :

We can set the file path and pattern in the application.properties file .

logging.file.name = error.loglogging.pattern.file= %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){green} [%level] %c{1.} [%t] %m%n

The logs get stored in the “error.log” file if it exists , or else a new file is created.

error.log

The logs keep getting stored in the file and updates itself every time the code is executed.

Summary

  • Every logging framework has three elements — Logger , formatter , Handler
  • With logger methods , we can log messages at all five log level supported by Spring Boot — ERROR , DEBUG, TRACE , INFO , WARNING
  • Using the Logger Factory class , we can generate loggers and print them on the console or save them in a file with formatting according to our requirements.

Code : dsrao711/LogginginSpringBoot: Understanding logging in SpringBoot (github.com)

Connect with me on LinkedIn : Divya Rao | LinkedIn

--

--