How can I set the logging level with application.properties?

Learn how can i set the logging level with application.properties? with practical examples, diagrams, and best practices. Covers java, spring, spring-boot development techniques with visual explana...

Mastering Spring Boot Logging Levels with application.properties

Hero image for How can I set the logging level with application.properties?

Learn how to effectively configure and manage logging levels in your Spring Boot applications using the application.properties file, from basic settings to environment-specific configurations.

Logging is a critical aspect of any application, providing insights into its runtime behavior, helping with debugging, and monitoring. Spring Boot, with its opinionated approach, simplifies logging configuration significantly. By default, Spring Boot uses Logback for logging, which is highly configurable. The primary way to manage logging levels in a Spring Boot application is through the application.properties (or application.yml) file. This article will guide you through setting logging levels for your entire application, specific packages, and even for different environments.

Understanding Logging Levels

Before diving into configuration, it's essential to understand the standard logging levels. These levels indicate the severity or importance of a log message. Logback, and most logging frameworks, follow a hierarchy where enabling a certain level also enables all levels above it. The standard levels, from least to most severe, are:

  • TRACE: Fine-grained informational events, most detailed.
  • DEBUG: Fine-grained informational events that are most useful to debug an application.
  • INFO: Informational messages that highlight the progress of the application at a coarse-grained level.
  • WARN: Potentially harmful situations.
  • ERROR: Error events that might still allow the application to continue running.
  • FATAL: Very severe error events that will presumably lead the application to abort.
  • OFF: Turn off logging.
flowchart TD
    OFF --> FATAL
    FATAL --> ERROR
    ERROR --> WARN
    WARN --> INFO
    INFO --> DEBUG
    DEBUG --> TRACE
    TRACE --> ALL

    subgraph Hierarchy
        OFF
        FATAL
        ERROR
        WARN
        INFO
        DEBUG
        TRACE
    end

    style Hierarchy fill:#f9f,stroke:#333,stroke-width:2px
    classDef levelStyle fill:#e0e0e0,stroke:#333,stroke-width:1px;
    class OFF,FATAL,ERROR,WARN,INFO,DEBUG,TRACE levelStyle;

Logging Level Hierarchy: Enabling a level includes all levels above it.

Setting Global Logging Level

You can set a global logging level for your entire Spring Boot application. This is useful for quickly changing the verbosity of your application's output. By default, Spring Boot logs at the INFO level. To change this, you use the logging.level.root property in your application.properties file.

logging.level.root=DEBUG

Setting the global logging level to DEBUG

With this configuration, all log messages from your application and its dependencies that are at DEBUG level or higher (INFO, WARN, ERROR, FATAL) will be printed to the console. If you set it to INFO, DEBUG and TRACE messages will be suppressed.

Configuring Logging for Specific Packages or Classes

Often, you'll want to have more granular control over logging. For instance, you might want to see DEBUG messages for your own application code but keep third-party libraries at INFO or WARN to avoid excessive log output. You can achieve this by specifying the logging level for a particular package or even a specific class.

logging.level.root=INFO
logging.level.com.example.myapp=DEBUG
logging.level.org.springframework.web=WARN

Setting logging levels for specific packages

In this example:

  • The global logging level is set to INFO.
  • The package com.example.myapp (which presumably contains your application's code) is set to DEBUG, meaning you'll see more detailed logs from your own classes.
  • The org.springframework.web package is set to WARN, suppressing INFO and DEBUG messages from Spring Web components, which can be quite verbose.

Environment-Specific Logging

Spring Boot's externalized configuration allows you to define different logging levels for various environments (e.g., development, test, production). This is typically done using profile-specific application.properties files, such as application-dev.properties or application-prod.properties.

# application.properties (default profile)
logging.level.root=INFO
logging.level.com.example.myapp=INFO

# application-dev.properties (for development environment)
logging.level.root=DEBUG
logging.level.com.example.myapp=TRACE

# application-prod.properties (for production environment)
logging.level.root=WARN
logging.level.com.example.myapp=ERROR

Environment-specific logging configurations

To activate a specific profile, you can use the spring.profiles.active property, for example, by running your application with java -jar myapp.jar --spring.profiles.active=dev or by setting it in your application.properties file:

spring.profiles.active=dev