Customizing Logging in Spring Boot: Log Level, Root Logger, and More

Logging is the means of recording the events that occur in an application. It is an essential tool for debugging and troubleshooting issues in an any application. I will talk about the logging in Spring Boot application as an example.
In a Spring Boot application, logging is typically handled by a logging framework such as Logback or Log4j.
The logging framework uses loggers to write log messages to different destinations such as the console, a file, or a database. Each logger is associated with a name, and the log messages are sent to the logger with that name. For example, a logger named “com.mycompany.myapp” will log messages for classes in the “com.mycompany.myapp” package.
The log level hierarchy is a way to control the amount of information that is logged. The levels, in decreasing order of severity, are:
- ERROR: Only log messages that indicate an error has occurred.
- WARN: Log messages that indicate a potential problem.
- INFO: Log messages that provide information about the application’s normal operation. -DEBUG: Log messages that provide detailed information about the application’s operation.
- TRACE: Log messages that provide the most detailed information about the application’s operation.

When you specify a log level of DEBUG, all log levels above DEBUG will be considered. This means that log messages of level DEBUG, INFO, WARN and ERROR will be logged. But the TRACE level will not be considered.
For example, if a developer sets the log level to DEBUG and an error occurs in the application, the log message for that error will be written to the log file with level ERROR, but also log message with levels WARN, INFO and DEBUG will be logged too.
By default, Spring Boot sets the log level to INFO, which means that log messages of level INFO and above will be logged. Developers can change the log level by configuring the logging framework in the application’s properties file.
Overriding the log level means changing the log level for a specific logger. For example, if a developer wants to change the log level for the “com.mycompany.myapp” logger to DEBUG, they can add the following line to the application’s properties file:
logging.level.com.mycompany.myapp=DEBUG
This will cause log messages of level DEBUG and above to be logged for the “com.mycompany.myapp” logger.
It is important to mention that the root logger controls the logging for the entire application. It is the parent of all other loggers and is configured by default with a level of INFO. you can change the root logger level by adding the following line to the application’s properties file:
logging.level.root=DEBUG
this will cause log messages of level DEBUG and above to be logged for the root logger and all other loggers that haven’t been overridden.
Here is an example of a Spring Boot logging configuration in the application.properties file.
logging.level.root=DEBUG
logging.level.com.mycompany.myapp=INFO
logging.file=logs/application.log
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%nIn this example, the root logger level is set to DEBUG and the “com.mycompany.myapp” logger level is set to INFO. The log messages will be written to the “logs/application.log” file and will include the date, level, logger name, and message.
There are many ways to customize logging in a Spring Boot application. Developers can change the log level, configure the logging framework to log messages to different destinations, and add custom log patterns. Some other common configuration options include:
- logging.path: The directory where log files are written.
- logging.file: The name of the log file.
- logging.pattern: The pattern used to format log messages.
- logging.exception-conversion-word: The word used to represent an exception in a log message.
It’s worth mentioning that Spring Boot also provides a default configuration for the most common use cases, which means that developers do not need to configure logging in most cases, but in some cases it might be necessary to add some configurations to suit the specific needs of the application.
For more information on logging in Spring Boot, see the following links:
- Spring Boot Logging: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging
- Logback Configuration: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-logback
- Log4j2 Configuration: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging
So the logging is an essential tool for debugging and troubleshooting issues in a Spring Boot application. The logging framework uses loggers to write log messages to different destinations. The log level hierarchy controls the amount of information that is logged. Developers can change the log level, configure the logging framework to log messages to different destinations, and add custom log patterns. The root logger controls the logging for the entire application and can be overridden to change its level of logging.
Please follow me for contents and updates.






