HomeSpringSpringBoot7 easy steps to implement Springboot logging in the application

7 easy steps to implement Springboot logging in the application

Logging is one of the key features of any enterprise program

Logging is one of the key features of any enterprise program, along with a few configuration details, we can get an understanding of the Spring Boot Logging mechanism in this article.

Introduction of Springboot Logging 

Spring Boot has several ready-to-use features and Logging is one of those features, using Commons Logging for its internal logging by default, but it also offers options for using and configuring every other log function. If we use Spring Boot Starters for our program, Logback would be used by default for logging, unless we choose to use some other logging API, such as Log4J2.

Understanding of Springboot Logs

Let’s take a brief look at the default log out for the SpringBoot application before we get into more details, to grasp it more clearly.

12:50:04.275 [main] DEBUG com.onurdesk.Application - debug message
12:50:04.319 [main] INFO com.onurdesk.Application - This is an info message
12:50:04.319 [main] WARN com.onurdesk.Application - Warning for this application
12:50:04.319 [main] ERROR com.onurdesk.Application - Seems some error in the application
12:50:04.728 [restartedMain] DEBUG com.onurdesk.Application - debug message
12:50:04.728 [restartedMain] INFO com.onurdesk.Application - This is an info message
12:50:04.728 [restartedMain] WARN com.onurdesk.Application - Warning for this application
12:50:04.728 [restartedMain] ERROR com.onurdesk.Application - Seems some error in the application

How to configure Springboot logging in the controller?

To understand how to setup and monitor Spring Boot Logging, let’s build a basic Controller with a few log statements.

package com.onurdesk;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//How we use Springboot logging 
// at onurdesk.com

@RestController
public class DemoLoggingController {
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    @GetMapping(value = "/demo/greeting")
    public String test(){

        LOG.debug("debug message");
        LOG.info("This is an info message");
        LOG.warn("Warning for this application");
        LOG.error("Seems some error in the application");
        return "test";
    }
}

By opening http:/localhost:8080/demo/greeting above the program,

7 easy steps to implement Springboot logging in the application

we can see the following output in the terminal.

2020-11-17 13:22:23 - Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-11-17 13:22:23 - Initializing Servlet 'dispatcherServlet'
2020-11-17 13:22:23 - Completed initialization in 5 ms
2020-11-17 13:22:23 - Warning for this application
2020-11-17 13:22:23 - Seems some error in the application

The Spring Boot Logging API offers a range of features that help us decouple our code

  • We use the SFL4J facade for our logging, so we are decoupled from the underlying Logback API.
  • Through doing this we are free to substitute Logback with some other logging API without modifying our codebase.

Log File Output for Springboot logging application

By default, the Spring Boot Logging API logs output to the console and not to any file, to write log output to a file, we should set the logging.file or logging.path properties in the application.properties file.

application.log file

logging.level.com.onurdesk=WARN

# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

# Logging pattern for file
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

logging.file=app.log

Please note that if we use the logging.path object, Spring Boot will write a file with the name spring.log to the designated directory.

The setting of log level for Springboot logging application

Spring Boot offers a simple way to customize and adjust the logging standard for your program. tWe will use the application.properties file to configure the desired logging level for our application using ‘logging.level.*=LEVEL.‘ Let’s use our previous example to explain the log level settings in our program. We will customize our log level to just produce Alert and ERROR logs.

In application.properties

logging.level.com.onurdesk=WARN

By opening http:/localhost:8080/demo/greeting above the program, we can see the following output in the terminal.

2020-11-17 13:22:23 - Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-11-17 13:22:23 - Initializing Servlet 'dispatcherServlet'
2020-11-17 13:22:23 - Completed initialization in 5 ms
2020-11-17 13:22:23 - Warning for this application
2020-11-17 13:22:23 - Seems some error in the application

Configure Logback Through External File for Springboot logging application

For most systems, Spring Boot Logging default settings are more than adequate, but for large-scale business applications, there are specific logging specifications, and Spring Boot provides a way to customize them with an external XML format.

You should place a logback.xml or logback-spring.xml file in the root of your classpath that will be picked up from there by Spring Boot. Please notice that logback-spring.xml is favored by Spring Boot over the logback.xml format.

here is a sample logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="com.onurdesk.rest" level="WARN" additivity="false">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </logger>
</configuration>

Spring Boot Profiles in Springboot Logging application

Spring Profiling is an outstanding idea that allows us versatility in identifying properties for different conditions without any code changes. Spring Boot provides the same profile mechanism for logback setup using the feature. Let’s use an example to explain how you can identify different logging levels for DEV and Production environments using the same logback setup.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <springProfile name="dev">
        <logger name="com.onurdesk.rest" level="DEBUG" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>>
    </springProfile>
    <springProfile name="production">
        <logger name="com.onurdesk.rest" level="WARN" additivity="false">
            <appender-ref ref="FILE" />
        </logger>
    </springProfile>
 </configuration>

Final Summary of Springboot logging application

In this article, we looked at the various features of the Spring Boot Logging API. We addressed the default settings offered by Spring Boot along with the ways to modify or customize these options. Logging is one of the core tools in application creation because, for large business systems, logging specifications can become time-consuming, but the Spring Boot API includes all tools to manage all of these complex applications with limited configuration changes.

If you’re starting Spring Boot, please read the Spring Boot application to launch your Spring Boot journey.

Admin
Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

Recent Comments