Spring Boot
Logging Configuration
Environment Variables
Application Properties
Log Level

Set Logging Level in Spring Boot via Environment Variable

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Spring Boot is a widely-used framework to create standalone, production-grade Spring-based applications with ease. One of the critical aspects of maintaining and debugging a Spring Boot application is managing the logging levels. Adjusting the logging levels at runtime without altering the configuration files can be crucial for seamless operations and problem-solving. Spring Boot allows setting the logging levels dynamically using environment variables, providing a straightforward way to tailor the log output according to the needs of different environments or situations.

Logging Levels in Spring Boot

Spring Boot supports various logging levels that dictate the granularity and verbosity of the logs produced by the application. The primary logging levels, in the order of increasing severity, are:

  1. TRACE: Fine-grained informational events that are most useful to debug the application.
  2. DEBUG: Useful for debugging; includes detailed information about the application flow.
  3. INFO: Default setting; provides a standard log output showing application progress.
  4. WARN: Highlights undesirable or unexpected situations which are not necessarily errors.
  5. ERROR: Indicates significant issues that may lead to a halting of the application or a component.
  6. FATAL: Very severe error events that will presumably lead the application to abort.

Configuring Logging Levels using Environment Variables

Basics

Spring Boot can configure logging levels using the application.properties or application.yml files. However, adjusting logging levels directly in these files requires redeploying the application for changes to take effect, which is not ideal for production environments. Instead, using environment variables simplifies dynamic configuration without the hassle of file changes and redeployment.

Setting Logging Level via Environment Variable

To set a logging level using environment variables, the variable should be prefixed with LOG_LEVEL_, followed by the package or class name for finer control, or let it affect the whole application by using the root logger.

Examples

  1. Setting Root Logger Level:
    To set the root logger level to DEBUG, use:
bash
   export LOG_LEVEL_ROOT=DEBUG
  1. Package-Specific Logger Level:
    You can define logging levels specific to a package. For instance, to set the logging level for com.example.service to TRACE, use:
bash
   export LOG_LEVEL_COM_EXAMPLE_SERVICE=TRACE

Application of Environment Variables

In practice, you must ensure that the environment variables are available to the environment that runs your Spring Boot application. This usually means setting them in the host operating system or the orchestration system (like Kubernetes or Docker) used to manage the application's deployment.

Example with Docker

When deploying with Docker, you can set environment variables in the Dockerfile or during container startup:

dockerfile
# Dockerfile example
ENV LOG_LEVEL_ROOT DEBUG

Or start the Docker container with:

bash
docker run -e LOG_LEVEL_ROOT=INFO my-spring-boot-app

System Properties Overriding Environment Variables

In some scenarios, you may set logging levels during the application startup using JVM system properties which can also override environment variables. The syntax follows:

bash
java -DLOG_LEVEL_ROOT=ERROR -jar myapp.jar

Precedence Order

When configuring logging levels, Spring Boot considers system properties and environment variables' precedence in the following order:

  1. System properties (e.g., via -D flags)
  2. Environment variables

The precedence allows more flexibility to alter settings based on the scope of deployment.

Conclusion

By setting logging levels using environment variables, Spring Boot provides an efficient way to control logging granularity and verbosity dynamically. It helps manage and configure applications seamlessly across different environments, enhancing operations' quality and speed without the need for code changes or redeployment. Furthermore, keeping in mind the precedence of system properties and environment variables ensures fine-tuned control over logging across various deployment scenarios.

Summary Table

Configuration ApproachExampleApplicable ScopeOverrides
Logging Level RootLOG_LEVEL_ROOT=DEBUGEntire ApplicationYes, via system properties
Package-Specific LoggerLOG_LEVEL_COM_EXAMPLE_SERVICE=TRACESpecific Package/NamespaceYes, via system properties
Docker Environment Variable-e LOG_LEVEL_ROOT=INFOContained Application InstanceSystem properties in container
System Property-DLOG_LEVEL_ROOT=ERRORJVM InstanceHighest precedence

This table summarizes the methods to configure logging levels and highlights their scope and precedence, aiding in selecting the right approach for various deployment scenarios.


Course illustration
Course illustration

All Rights Reserved.