Set Logging Level in Spring Boot via Environment Variable for Flexible Debugging and Production Monitoring

A complete guide on controlling Spring Boot logging levels using environment variables for smoother development and deployment.3 min


Set-Logging-Level-in-Spring-Boot-via-Environment-Variable-for-Flexible-Debugging-and-Production-Monitoring

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 DEBUG logs for troubleshooting, while production should stick to INFO or WARN to 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 INFO level.

  • Log files are not created unless explicitly configured.

  • Root logger level can be adjusted via application.properties or application.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.rootLOGGING_LEVEL_ROOT

  • logging.level.org.springframework.webLOGGING_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

  1. Use INFO in production: It provides enough detail without overwhelming log storage.

  2. Enable DEBUG only when required: Debug logs can grow rapidly and increase costs in log aggregation systems.

  3. Keep sensitive data out of logs: Always sanitize logs to avoid exposing passwords, tokens, or personal information.

  4. Leverage profiles: Combine environment variables with Spring profiles (dev, test, prod) for consistent environments.

  5. 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 DEBUG or TRACE enabled 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.


Like it? Share with your friends!

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
0
Cute
Love Love
0
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
0
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Novice

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment