0
0
SpringbootHow-ToBeginner · 4 min read

How to Configure Logging in Spring Boot: Simple Guide

In Spring Boot, configure logging by setting properties in application.properties or application.yml files using keys like logging.level to control log levels. You can also customize log patterns and output destinations easily without extra code.
📐

Syntax

Spring Boot uses properties to configure logging. The main syntax involves setting log levels and patterns in application.properties or application.yml.

  • logging.level.=: Sets the log level (e.g., TRACE, DEBUG, INFO, WARN, ERROR) for a specific logger or package.
  • logging.pattern.console: Defines the log message format for console output.
  • logging.file.name or logging.file.path: Specifies the file name or directory for log files.
properties
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
logging.file.name=app.log
💻

Example

This example shows how to configure logging levels and output a log file in Spring Boot using application.properties. It sets the root log level to INFO, enables DEBUG for Spring Web, customizes the console log format, and writes logs to app.log.

properties/java
# application.properties
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
logging.file.name=app.log

// Sample Java class
package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        logger.info("Application started");
        logger.debug("Debugging Spring Web logs");
        logger.error("An error occurred");
    }
}
Output
2024-06-01 12:00:00 - Application started 2024-06-01 12:00:00 - Debugging Spring Web logs 2024-06-01 12:00:00 - An error occurred
⚠️

Common Pitfalls

Common mistakes when configuring logging in Spring Boot include:

  • Setting incorrect log levels that are too restrictive or too verbose.
  • Forgetting to specify the correct logger name (e.g., using logging.level.root vs. package-specific levels).
  • Misconfiguring file paths causing logs not to be saved.
  • Overriding default patterns without understanding the format syntax.

Always check your configuration file syntax and restart the application after changes.

properties
logging.level.org.springframework=TRACE  # Too verbose, may flood logs

# Correct approach
logging.level.org.springframework=INFO
📊

Quick Reference

Here is a quick cheat sheet for common logging properties in Spring Boot:

PropertyDescriptionExample
logging.level.Set log level for a logger or packagelogging.level.com.example=DEBUG
logging.pattern.consoleCustomize console log formatlogging.pattern.console=%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
logging.file.nameSet log file namelogging.file.name=app.log
logging.file.pathSet directory for log fileslogging.file.path=/var/logs
logging.level.rootSet default log levellogging.level.root=INFO

Key Takeaways

Configure logging in Spring Boot via application.properties or application.yml using logging.level and logging.pattern keys.
Set specific log levels per package or root to control verbosity effectively.
Customize log output format and file location easily without extra code.
Avoid overly verbose log levels to keep logs readable and useful.
Always restart your Spring Boot app after changing logging settings.