When your Spring Boot application grows in complexity, logging quickly becomes one of the most powerful tools at your disposal. But managing log levels across development, testing, and production environments can be challenging if you stick to hard-coded configuration files. Fortunately, Spring Boot provides a way to set logging levels dynamically via environment variables, giving developers and DevOps teams the flexibility to monitor and debug applications without touching code or redeploying.
In this article, we will explore why environment-based logging configuration matters, how it works in Spring Boot, and practical steps (with code examples) to set it up. Whether you are deploying in Docker, Kubernetes, AWS, or on your local machine, this method ensures you maintain full control over application logs.
Why Use Environment Variables for Logging in Spring Boot?

Before diving into the technical setup, it’s important to understand the “why”:
-
Environment-specific logging: Development may need
DEBUGlogs for troubleshooting, while production should stick toINFOorWARNto avoid log noise. -
No redeployment required: Changing log levels via environment variables avoids rebuilding or repackaging your Spring Boot application.
-
Cloud & container friendly: Modern platforms like Kubernetes, Docker, and AWS ECS rely heavily on environment variables for configuration.
-
Security and compliance: Keeping configurations external reduces the risk of exposing sensitive data inside code repositories.
The Default Logging Behavior in Spring Boot
By default, Spring Boot uses Spring Boot Logging System (which supports Logback, Log4j2, or Java Util Logging) with the following defaults:
-
Console output is set to
INFOlevel. -
Log files are not created unless explicitly configured.
-
Root logger level can be adjusted via
application.propertiesorapplication.yml.
For example, in application.properties:
logging.level.root=INFO logging.level.org.springframework.web=DEBUG
While this works, it’s not flexible for real-time production changes. That’s where environment variables step in.
Setting Logging Level via Environment Variable
Spring Boot automatically maps environment variables to configuration properties. To set a logging level, you can use:
1. Directly via SPRING_APPLICATION_JSON
You can pass JSON through the environment variable SPRING_APPLICATION_JSON:
export SPRING_APPLICATION_JSON='{"logging":{"level":{"root":"DEBUG"}}}'Now, when the application runs, it will use the DEBUG level globally.
2. Using Standard Environment Variable Convention
Spring Boot allows converting property names into environment variables using uppercase letters and underscores. For example:
export LOGGING_LEVEL_ROOT=DEBUG export LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=TRACE
Here:
logging.level.root→LOGGING_LEVEL_ROOTlogging.level.org.springframework.web→LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB
This method is widely used in Docker and Kubernetes deployments.
Example: Changing Logging Levels in Docker
Imagine you have a Spring Boot app packaged into a Docker container. You can control logging without modifying the image:
FROM eclipse-temurin:17-jdk COPY target/myapp.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Now, when running the container:
docker run -e LOGGING_LEVEL_ROOT=ERROR -e LOGGING_LEVEL_COM_MYAPP=DEBUG myapp:latest
This sets the root level to ERROR while keeping your custom package at DEBUG.
Example: Logging Levels in Kubernetes Deployment
In Kubernetes, environment variables are commonly set inside Deployment YAML files:
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-app
spec:
replicas: 2
template:
spec:
containers:
- name: springboot-container
image: myregistry/springboot-app:latest
env:
- name: LOGGING_LEVEL_ROOT
value: "INFO"
- name: LOGGING_LEVEL_COM_MYAPP_SERVICE
value: "DEBUG"With this setup, you can adjust logs across multiple pods simply by updating the Deployment spec.
Advanced Use Case: Dynamic Log Levels at Runtime
Spring Boot Actuator also allows runtime changes to log levels via its /actuator/loggers endpoint. Combined with environment variables, you can set a default log level at startup and adjust specific classes or packages dynamically.
Example request:
curl -X POST "http://localhost:8080/actuator/loggers/com.myapp.service"
-H "Content-Type: application/json"
-d '{"configuredLevel": "TRACE"}'This avoids restarts altogether, which is critical for high-availability applications.
Best Practices for Managing Logging Levels
Use INFO in production: It provides enough detail without overwhelming log storage.
Enable DEBUG only when required: Debug logs can grow rapidly and increase costs in log aggregation systems.
Keep sensitive data out of logs: Always sanitize logs to avoid exposing passwords, tokens, or personal information.
Leverage profiles: Combine environment variables with Spring profiles (
dev,test,prod) for consistent environments.Centralize logs: Use ELK (Elasticsearch, Logstash, Kibana), Grafana Loki, or AWS CloudWatch for observability.
Common Pitfalls and How to Avoid Them
Wrong variable format: Ensure you replace dots (
.) with underscores (_) when setting environment variables.Case sensitivity: Environment variable names must be uppercase.
Overlogging in production: Avoid leaving
DEBUGorTRACEenabled in production—it can cause performance bottlenecks.
Final Thoughts
Configuring logging levels via environment variables in Spring Boot is more than just a convenience—it’s a best practice in today’s cloud-native development world. By externalizing log configuration, you make your application portable, secure, and adaptable across environments.
Whether you are running locally, inside Docker, or orchestrating containers in Kubernetes, using environment variables keeps your logging strategy consistent and flexible. Combined with runtime log management via Spring Boot Actuator, you gain complete visibility and control over your application’s behavior.
By following the approaches outlined in this article, you’ll ensure your Spring Boot projects remain easier to debug, monitor, and maintain—without sacrificing agility in modern deployments.
Discover more from 9Mood
Subscribe to get the latest posts sent to your email.















0 Comments