How can I set the logging level with application.properties?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Configuring the logging level in a Spring Boot application using the application.properties file is a critical aspect of managing the verbosity of logs, enabling you to control what information is recorded about the application's operation. In this article, we will delve into how you can set the logging level, provide examples for various logging frameworks, and offer insights into best practices for managing logging effectively in your applications.
Understanding Logging Levels
Logging levels signify the severity or importance of the log messages. Configurations usually include:
- ERROR: Captures error events that might allow the application to continue running.
- WARN: Logs potentially harmful situations.
- INFO: Provides informational messages that highlight the progress of the application.
- DEBUG: Offers detailed information on the flow through the system, primarily for debugging purposes.
- TRACE: Captures the most detailed messages, providing the highest volume of logs but also the most granular information.
Configuring Logging Levels Using application.properties
Basic Configuration
Spring Boot uses Logback as the default logging implementation. You can configure the logging levels for different packages through the application.properties file as follows:
This configuration allows you to have finer control over log levels, enabling you to increase logging in specific areas without affecting the entire application.
Log Output Configuration
Aside from setting log levels, you can also configure the destination of log output, i.e., console or file:
Advanced Logging Configurations
Using YAML for Configuration
If you prefer YAML over properties files, you can achieve the same setup using application.yml:
Integration with Other Logging Frameworks
Log4j2 Configuration
If you prefer using Log4j2, you need to exclude spring-boot-starter-logging and include spring-boot-starter-log4j2 in your pom.xml or build.gradle. Here's a basic setup for application.properties using Log4j2:
Key Points Summary
| Configuration Aspect | Properties Example | Default Framework | Logging Output Destination |
| Root Level Log | logging.level.root=INFO | Logback | Console/File |
| Package/Class Level | logging.level.com.example=DEBUG | Logback | Console/File |
| File Output | logging.file.name=app.log logging.file.max-size=10MB logging.file.max-history=15 | Logback | File with policies |
| YAML Configuration | logging: level: root: INFO | Logback | Console/File |
| Log4j2 Configuration | logging.level.root=ERROR logging.log4j2.file.name=log4j2-app.log | Log4j2 | Console/File |
Best Practices
- Appropriate Levels: Ensure that you set different log levels appropriately. For production, ERROR or WARN levels are generally sufficient, while DEBUG may be appropriate for development and testing environments.
- Performance Considerations: Be cautious with extensive logging at the DEBUG or TRACE levels in production, as it can lead to performance degradation and excessive log file sizes.
- Security Concerns: Avoid logging sensitive information such as passwords, tokens, or personal data to protect user privacy and adhere to compliance requirements such as GDPR.
In conclusion, proper logging is vital for diagnosing issues and understanding application behavior. Configuring logging levels correctly using application.properties ensures clarity, efficiency, and control over log output, making it indispensable for better application management.
Related reading
- How can I start spring boot application in docker with profile?
- How can I store a binary file in a Kubernetes ConfigMap?
- How can I trigger a Kubernetes Scheduled Job manually?
- How can I update a secret on Kubernetes when it is generated from a file?
- How can I set up a letsencrypt SSL certificate and use it in a Spring Boot application?
- How can I solve java.lang.NoClassDefFoundError?
- How can I use an initContainer to set an environment variable or modify the launch command of the main container?
- How can I use environment variables in docker-compose?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.