Logging with Spring Boot

Let’s see how to leave log records in Spring Boot.

Spring Boot has Log4j libraries and uses SLF4J.  This logging framework can be used with zero configuration very quickly.

So let’s see how easy it is to leave log records with Spring Boot.

How to create logs with Spring Boot

First, create a LOG using a LoggerFactory

// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
private final Logger LOG = LoggerFactory.getLogger(LogExampleController.class);

Now, you can use LOG to registry messages.

LOG.trace("A TRACE Message");
LOG.debug("A DEBUG Message");
LOG.info("An INFO Message");
LOG.warn("A WARN Message");
LOG.error("An ERROR Message");
// FATAL is not available in slf4j

Log with SLF4J

Note that we are using LoggerFactory. This LoggerFactory is part of SLF4J

If we read the documentation from LoggerFactory:

The LoggerFactory is a utility class producing Loggers for various logging APIs, most notably for log4j, logback and JDK 1.4 logging

In other words, SLF4J is a facade to unify different log frameworks.

Log with Log4j (without SLF4J)

If we wanted to use Log4j directly we would have to create the LOG in this way.

// import org.apache.logging.log4j.LogManager;
// import org.apache.logging.log4j.Logger;
private static final Logger LOG = LogManager.getLogger(Log4jExampleController.class);

After, use LOG to registry messages.

LOG.trace("A TRACE Message without going through SLF4J");
LOG.debug("A DEBUG Message without going through SLF4J");
LOG.info("An INFO Message without going through SLF4J");
LOG.warn("A WARN Message without going through SLF4J");
LOG.error("An ERROR Message without going through SLF4J");
// FATAL is available in Log4j
LOG.fatal("A FATAL Message without going through SLF4J");

Which log levels can we use?

In the log, we have different levels that we can use depending of the type of message to be logged

  • TRACE: this level is used to log events in great detail, superior to DEBUG.
  • DEBUG: this level is used for logging when you ‘DEBUG’ your application. They are more useful to debug an application when you are developing.
  • INFO: The INFO level is used for informative messages related to the progress of the application and general messages.
  • WARN: The WARN level is used for situations that could be harmful or risky.
  • ERROR: The ERROR level is used to log error events within the application.
  • FATAL: The FATAL level is used for very severe error events that may cause the application to abort.

Log Priority

The priority of logs has this order: Trace has the lowest priority and Fatal has the highest priority.

//lowest  - - - - - - - - - - - - -  highest 
Trace < Debug < Info < Warn < Error < Fatal

How to change the log level in Spring Boot

By default, Spring configures the logs at the Info Level.

This means that it will display all Info logs and those with the highest priority to Info.

If you do not change the configuration, Spring will show logs from Info to Fatal

If we execute this Controller ‘http://localhost:8080/log

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

@RestController
public class LogExampleController {

    private final Logger LOG = LoggerFactory.getLogger(LogExampleController.class);

    @GetMapping("/log")
    public String logExample() {
        LOG.trace("A TRACE Message");
        LOG.debug("A DEBUG Message");
        LOG.info("An INFO Message");
        LOG.warn("A WARN Message");
        LOG.error("An ERROR Message");
        return "Hello Log";
    }
}

The output console looks like that

Spring Boot Log

To change the level, use the property file by setting the starting level

logging.level.root=ERROR

Logs lower than ERROR will be discarded.

Spring Boot Log

How to write log in a file

If you want to write logs into a file, you can add this property logging.file.name

This will create a file ‘logfile.log’.

logging.file.name=logfile.log

Executing the controller /log with log level INFO. The file will be created as shown below

Spring Boot Log

Rolling log files

Rolling log files means that log files will be recreated under some criteria.

This is useful to avoid having large files or log files with old information that is no longer necessary

You have to declare the following property logging.logback.rollingpolicy.file-name-patter in your application.properties file to activate the rolling file.

With this property, a new file will be created every day and the previous one will be renamed.

logging.file.name=logfile.log
logging.logback.rollingpolicy.file-name-pattern=logfile-%d{yyyy-MM-dd}.%i.log

Example, output for old files:

logfile.log ← current log file
logfile-2022-08-10.0.log ← files from previous days
logfile-2022-08-08.0.log
logfile-2022-08-07.0.log

Max size

This will set the maximum log file size to 1MB. If this size is exceeded, a new log file will be created. The default size is 10MB.

logging.file.name=logfile.log
logging.logback.rollingpolicy.file-name-pattern=logfile-%d{yyyy-MM-dd}.%i.log
logging.logback.rollingpolicy.max-file-size=1MB

Example, output files:

logfile.log ← current log file, less than 1MB
logfile-2022-08-11.0.log ← Log of the day. Reached 1MB
logfile-2022-08-11.1.log ← Log of the day. Reached 1MB

There are other properties that are very useful that I often use.

logging.logback.rollingpolicy.max-history ← Maximum number of archive log files to keep. (default 7)
logging.logback.rollingpolicy.total-size-cap ← Total size of log backups to be kept.

Please check the official documentation for the other properties.

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html

Conclusion

We have learned how to create log files with Spring Boot that we can use during our developing task or during production execution.

We understood the different levels of logs also how to create files with these registries.

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also