How to set logging levels in spring boot


Spring Boot uses the popular logging framework, Logback, for its logging capabilities. Logback is a robust and flexible logging framework that can be customized to meet your logging needs.

To set logging levels in Spring Boot, you can use the application.properties or application.yml configuration files. Here’s how to configure logging levels using these files:

application.properties

Copy codelogging.level.root=WARN
logging.level.org.springframework.web=DEBUG

In this example, we are setting the logging level for the root logger to WARN, which means that only messages of level WARN, ERROR, and FATAL will be logged. We are also setting the logging level for the org.springframework.web logger to DEBUG, which means that messages of levels DEBUG, INFO, WARN, ERROR, and FATAL will be logged for that logger.

application.yml

yamlCopy codelogging:
  level:
    root: WARN
    org.springframework.web: DEBUG

This YAML example is equivalent to the application.properties configuration shown above.

In addition to setting logging levels for specific loggers, you can also set the logging level for the entire application by setting the logging.level.* property in application.properties or application.yml. For example:

Copy codelogging.level.=INFO

In this example, we are setting the logging level for the entire application to INFO, which means that messages of levels INFO, WARN, ERROR, and FATAL will be logged for all loggers.

You can also configure logging levels programmatically by creating a Logger object and calling its setLevel method. However, it’s generally recommended to use the application.properties or application.yml configuration files to configure logging levels in Spring Boot, as this provides a more flexible and maintainable way of managing logging levels.