In this blog post, we will discuss how to set the logging level with application.properties in a Spring Boot application. Logging is an essential part of any application and helps to track and diagnose issues that may arise during development or production. In Spring Boot, the default logging framework is logback, and it can be configured using application.properties or application.yml files.
In this post, we will be using application.properties to set the logging level. We will also provide code examples to demonstrate how to set the logging level for different loggers and how to configure different logging levels for different packages.
Setting the Logging Level
The logging level can be set in application.properties by adding the following line:
logging.level.root=DEBUG
This sets the logging level for the root logger to DEBUG. The available logging levels are TRACE, DEBUG,INFO,WARN,ERROR,FATAL, and OFF.
Setting the Logging Level for Specific Loggers
To set the logging level for a specific logger, use the following syntax:
logging.level.<logger-name>=<level>
For example, to set the logging level for the org.springframework package to DEBUG, use the following line:
logging.level.org.springframework=DEBUG
Setting the Logging Level for Specific Packages
To set the logging level for all loggers in a specific package, use the following syntax:
logging.level.<package-name>=<level>
For example, to set the logging level for all loggers in the com.example package to DEBUG, use the following line:
logging.level.com.example=DEBUG
Code Examples
Here is a sample application.properties file that sets the logging level for the root logger to DEBUG and for the org.springframework package to INFO:
logging.level.root=DEBUG
logging.level.org.springframework=INFO
And here is an example of how to set the logging level for a specific logger:
logging.level.com.example.MyLogger=DEBUG
Using application.yml
In addition to application.properties, Spring Boot also supports using YAML files to configure the application. The file should be named application.yml or application-{profile}.yml. Here is an example of how to set the logging level to DEBUG in application.yml:
logging:
level:
root: DEBUG
Using a Custom Logback.xml
Another way to configure the logging framework in Spring Boot is to use a custom logback.xml file. This file should be placed in the classpath of the application. By default, Spring Boot will look for a file named logback-spring.xml, but you can specify a different file name using the following property: logging.config=<file-path>
. The file should be in logback’s XML format and can include various configuration elements such as appenders, encoders, and loggers.
Advanced Configuration
In addition to setting the logging level, you can also configure other aspects of the logging framework using application.properties. Here are a few examples:
- Configuring the log file location: You can set the location of the log file using the following property:
logging.file=<file-path>
. For example, to set the log file location to /var/log/myapp.log, use the following line:logging.file=/var/log/myapp.log
- Configuring the maximum log file size: You can set the maximum size of the log file using the following property:
logging.file.max-size=<size>
. The size can be specified in bytes, kilobytes, megabytes, or gigabytes. For example, to set the maximum log file size to 10 megabytes, use the following line:logging.file.max-size=10MB
- Configuring the number of log files to retain: You can set the number of log files to retain using the following property:
logging.file.max-history=<number>
. For example, to retain the last 10 log files, use the following line:logging.file.max-history=10
- Configuring the log pattern: You can set the log pattern used by the logging framework using the following property:
logging.pattern.console=<pattern>
. The pattern can include various placeholders, such as %d (date), %t (thread), %level (log level), %logger (logger name), and %msg (log message). For example, to set the log pattern to include the date, log level, logger name, and log message, use the following line:logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger - %msg%n
These are just a few examples of the many configuration options available. You can refer to the official documentation for a complete list of properties that can be used to configure the logging framework in Spring Boot.
Conclusion
In this blog post, we have discussed how to set the logging level with application.properties in a Spring Boot application. We have provided examples of how to set the logging level for different loggers, packages, and how to configure other aspects of the logging framework, such as log file location, maximum log file size, number of log files to retain, and log pattern. We also discussed how to use application.yml, and custom logback.xml to configure the logging framework.