logging configuration
application.properties
logging level
spring boot
java logging

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.

Practice system design

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:

properties
1# Sets the root logging level
2logging.level.root=INFO
3
4# Sets the logging level for a specific package
5logging.level.com.example=DEBUG
6
7# Configures a particular class
8logging.level.com.example.MyClass=TRACE

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:

properties
1# Log output to a file
2logging.file.name=app.log
3
4# Log output with rolling policy
5logging.file.name=app.log
6logging.file.max-size=10MB
7logging.file.max-history=15
8
9# Log pattern for console or file output
10logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

Advanced Logging Configurations

Using YAML for Configuration

If you prefer YAML over properties files, you can achieve the same setup using application.yml:

yaml
1logging:
2  level:
3    root: INFO
4    com.example: DEBUG
5    com.example.MyClass: TRACE
6  file:
7    name: app.log
8    max-size: 10MB
9    max-history: 15
10  pattern:
11    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

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:

properties
logging.level.root=ERROR
logging.level.com.example=DEBUG
logging.log4j2.file.name=log4j2-app.log

Key Points Summary

Configuration AspectProperties ExampleDefault FrameworkLogging Output Destination
Root Level Loglogging.level.root=INFOLogbackConsole/File
Package/Class Levellogging.level.com.example=DEBUGLogbackConsole/File
File Outputlogging.file.name=app.log logging.file.max-size=10MB logging.file.max-history=15LogbackFile with policies
YAML Configurationlogging: level: root: INFOLogbackConsole/File
Log4j2 Configurationlogging.level.root=ERROR logging.log4j2.file.name=log4j2-app.logLog4j2Console/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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.