EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Logging
 

Spring Boot Logging

Updated April 4, 2023

Spring Boot Logging

 

 

Introduction to Spring Boot Logging

Spring boot logging is defined as a framework that enables developers to trace out errors that might occur in the running of the application. Logging in spring boot is basically an API that provides tracing out of information along with a recording of any critical failure that might occur in the application during its run. Spring boot uses a common logging framework to implement all internal logging and leaves the log implementation open. A thin adapter that allows configurable bridging to other logging systems is the USP of Commons logging. The default configurations provided by spring boot logging are Java Util logging, Log4J2, Logback. The loggers in default configurations are pre-configured for using console output.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of Spring Boot Logging

Spring boot logging, as mentioned, is a logging framework that provides the flexibility of log implementation. For example, if the developer uses “Starters”, logback is used for logging. Logback routing is included in the framework to ensure that all the dependent libraries that uses the Java Util logging, Log4J2, Commons logging etc., work in resonance. Here we will see about spring boot logging from the syntax perspective.

1. Instantiating the log level for the root.

logging.level.root = <log level>

<log level> can be replaced by any of the following log levels as per the requirement of the application.

The log levels which are available are: “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”.

2. Specifying the log file path.

logging.path = /<path of the file>

3. Specifying the log file name.

logging.file = /<path of the file>/<log file name>.log

4. Importing the logger class.

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

5. Defining the logging.xml pattern.

<pattern> define the pattern here </pattern>

6. Defining the logging.xml pattern.

logging.pattern.console = <logging pattern goes here>

How does Logging work in Spring Boot?

Working on it is important to understand the format in which a log is printed.

A default spring boot log file contains the following items:

  • Date and Time: This provides the occurrence date and time of the log item.
  • Log Level: This provides the information on what level of information the log is of. It will be one out of the 7 options we will see now, i.e. TRACE, DEBUG, INFO, WARN, ERROR, FATAL or OFF.
  • Process ID: This provides the information of the process ID on which the spring boot application is running on.
  • Separator: — This is a separator which signifies the next part of the log.
  • Thread Name: This is enclosed within a square ( [ ] ) brackets and mostly contains the thread within which the logging thread or element is present.
  • Logger Name: This is the penultimate element that contains the source class name.
  • Log Message: Finally, this element contains the log message, which explains the methods followed in the application and helps us tracing back to the root cause if an error pops up in the application.

The 7 option falls in the following order of severity (low to high).

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL
  • OFF

The log level, as mentioned in the application, determines the detailing of the log messages. Configuring the log level with optimum aspect saves a lot of time. For example, when the level shows as FATAL, the application should actually have encountered some serious failure. Once the architecture of the application is developed, the logs should be carefully set up so that tracing back to the root point is easier. We don’t want to look at logs that mention some information about the process every time. It might be a possibility in cases this information is classified as FATAL due to improper architecture.

With each of the classified options available, when a code in the application executes, the corresponding options are validated against the level declared for the application; if the severity of the level declared for application is lower than the point of code execution, it is put in the log message. One can declare the level by using the code logging.level.com.<application name>=<logging level>. Now let us assume that during code execution, the code breaks and fails due to some corner cases we have not considered. The location of the code is put into the log message with the appropriate level. But if the code execution yields a less severe level than the declared level of application, the logging is skipped.

Once it is adjudged of what to put, the application looks for the pattern declared in either the xml or in the java code and corresponding elements are extracted and noted. A developer may also choose to output the log into the, and as per the path and file mentioned, the logs gets output to the file. Another element is that one can choose to have colour-coded log output in the console in case the terminal supports ANSI.

Example of Spring Boot Logging

Given below is the example of Spring Boot Logging:

Logging in Spring Boot with different cases.

Syntax:

Logresource.java:

package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
public class Logresource {
Logger logger = LoggerFactory.getLogger(Logresource.class);
@RequestMapping("/")
public String home() {
logger.debug("This is a DEBUG statement");
logger.warn("This is a WARN statement");
logger.error("This is a ERROR statement");
return "Hello Readers";
}
}

DemologgingApplication.java:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemologgingApplication {
public static void main(String[] args) {
SpringApplication.run(DemologgingApplication.class, args);
}
}

Output:

logging.level.com.example.demo=WARN is an application.properties file:

Spring Boot Logging 1

logging.level.com.example.demo=ERROR is an application.properties file:

demo=ERROR

logging.level.com.example.demo=TRACE is an application.properties file:

demo=TRACE

In this example, we saw that as if the logging severity is higher than the one initialized in the application.properties only that message is logged and rest others are ignored.

Conclusion

In conclusion, in this article, we have seen working on spring boot logging, and the flexibility spring boot provides to the developers. Following it is a small piece of code that tries to replicate the concept of the logging.

Recommended Articles

This is a guide to Spring Boot Logging. Here we discuss the introduction, how logging works in spring boot? And example, respectively. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW