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:
- TRACE: Fine-grained informational events that are most useful to debug the application.
- DEBUG: Useful for debugging; includes detailed information about the application flow.
- INFO: Default setting; provides a standard log output showing application progress.
- WARN: Highlights undesirable or unexpected situations which are not necessarily errors.
- ERROR: Indicates significant issues that may lead to a halting of the application or a component.
- 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
- Setting Root Logger Level:
To set the root logger level toDEBUG, use:
- Package-Specific Logger Level:
You can define logging levels specific to a package. For instance, to set the logging level forcom.example.servicetoTRACE, use:
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:
Or start the Docker container with:
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:
Precedence Order
When configuring logging levels, Spring Boot considers system properties and environment variables' precedence in the following order:
- System properties (e.g., via
-Dflags) - 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 Approach | Example | Applicable Scope | Overrides |
| Logging Level Root | LOG_LEVEL_ROOT=DEBUG | Entire Application | Yes, via system properties |
| Package-Specific Logger | LOG_LEVEL_COM_EXAMPLE_SERVICE=TRACE | Specific Package/Namespace | Yes, via system properties |
| Docker Environment Variable | -e LOG_LEVEL_ROOT=INFO | Contained Application Instance | System properties in container |
| System Property | -DLOG_LEVEL_ROOT=ERROR | JVM Instance | Highest 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.

