Overview

Logging is an important part of an application - particularly when trying to troubleshoot problems that occurred in the past.

In this article, we will discuss adding logging to a Spring Boot Java application. Spring contains the Logback logging framework, which we will use in our examples.

In previous articles, I showed how to create a Java Spring Boot application. We will continue working on that application in this article.

NOTE: The code for the previous article can be found here. If you did not work through the samples in that article, you can use this code as a starting point for this article.

Logging Levels

Logging supports five levels of logging. In order, they are:

  1. TRACE
  2. DEBUG
  3. INFO
  4. WARN
  5. ERROR

When you configure logging, you can specify a level of logging and your application will log only messages at that specified level and above. So, if you configure logging at the TRACE level, all five levels are logged; but, if you configure logging at the INFO level, only INFO, WARN, and ERROR messages are logged.

Logging Configuration

Add the file "logback-spring.xml" to any folder in your application path. I like to add it into the "resources" folder.

The following XML configures your application to implement a Console logger and tells Java how to format text in the console. Paste it into the logback-spring.xml file.



    
        
            
                %black(%d{ISO8601}) %highlight(%-5level) %cyan(%logger{36}) - %msg%n
            
        
    

    
        
    

The section tells what to log and where. We set level="INFO", telling our logger to log only those statements at the INFO level and above. This includes INFO, WARN, and ERROR logging statements. It will ignore any TRACE or DEBUG logging statements.

Within the element is an element. The ref attribute tells the application to use a logging appender named "Console". We can find the configuration for this appender near the top of the XML file. Inside the element is information on how to log to the console. The pattern tells what information to log and how to format it. In this case, we want the date, level, and log message. The pattern also includes formatting information, such as color and date formatting. This can be confusing, but you can find references on formatting here.

Code

Now that we have configured our application for logging, we can add some logging code.

In one of the controllers (I chose _GreetingController), create a logger object as a private variable, as shown below:

private Logger logger = LoggerFactory.getLogger(MathController.class);

NOTE: Validate that the Logger and the LoggerFactory classes are in the import org.slf4j libraries

The Logger class contains five methods that write to a log - each at a different level (trace, debug, info, warn, and error).

Add some logging statements to your code. These statements will work in the greetPerson method:

logger.trace("Hello" + personName);
logger.debug(personName +" is Tracy's mom!");
logger.info("Information, please, " + personName);
logger.warn("Warning! Warning! Warning! Dr, Smith, call " + personName);
logger.error(personName + " is exceptional!");

Running the Code

We can run this code locally and call the greetPerson method with the URL: http://localhost:8080/greetings/greet/David

After running, we should see log statements in the console, as shown in Fig. 1.

Log Output
Fig. 1

Note that we have five log statements, but only output three of them. This is because we configured the application to only log level INFO and above.

Conclusion

In this article, I showed you how to implement logging in an Azure Spring Boot application. You can find the sample code here.