Spring Boot logging pattern
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Spring Boot enhances the logging capabilities of standard Java applications by providing a default logging configuration that can be easily customized. Logging is an essential feature for diagnosing issues, understanding application flow, and monitoring system behavior. In this article, we will explore the logging patterns in Spring Boot, how to customize them, and some best practices.
Default Logging in Spring Boot
Logging Framework
Spring Boot uses SLF4J (Simple Logging Facade for Java) as a logging abstraction, with Logback as the default logging implementation. This combination allows for powerful logging capabilities with minimal configuration.
Default Configuration
When you create a new Spring Boot application, it comes pre-configured with a default logging setup:
- Logging output is sent to the console.
- The default log level is
INFO. - Log messages are preceded by a common pattern:
[Date] [Log level] [Process ID] [Thread name]: [Logger name]: Message.
For example, a log statement might look like this:
Logging Levels
Spring Boot supports several logging levels, defined in order of increasing severity:
TRACE: Detailed information, useful for diagnosing problems.DEBUG: Additional information while debugging.INFO: General application events (default level).WARN: Potential issues in the application.ERROR: Serious issues that could cause the application to stop running.FATAL: Critical problems that lead to a crash.
Customizing Logging Patterns
Application Properties
You can customize logging by modifying the application.properties or application.yml file. Some common properties include:
For application.properties:
For application.yml:
Logback Configuration
For more advanced configurations, you can provide your own logback-spring.xml file in the src/main/resources directory.
Here’s an example configuration:
Best Practices for Spring Boot Logging
- Use Appropriate Log Levels: Leverage different log levels to categorize messages. Avoid logging everything at
INFOorERRORlevels. - Separate Business and Infrastructure Logs: Customize logging such that business logic and infrastructure logs are separated, which can be achieved using custom appenders and loggers.
- Asynchronous Logging: Improve performance by using asynchronous logging. This can be configured in Logback to prevent logging blocking the application’s main thread.
- Rolling Logs: To prevent logs from consuming too much disk space, use a rolling log configuration to archive and rotate old log files.
Key Points Summary
| Feature | Default Behavior/Description | Customization Options |
| Logging Framework | SLF4J & Logback | Replaceable with other frameworks like Log4j |
| Default Output | Console | File and other appenders |
| Default Log Level | INFO | Set per package/class |
| Configuration File | N/A | application.properties/application.yml, logback-spring.xml |
| Asynchronous Logging | Synchronous by default | Configure AsyncAppender in Logback |
| Log Pattern | Basic date, level, message | Customizable through properties/XML |
Additional Details
External Libraries and Tools
- Spring Boot Admin: A tool for managing and monitoring Spring Boot applications. It provides real-time insight into log output among other features.
- ELK Stack: Integrate with Elasticsearch, Logstash, and Kibana for centralized logging, analysis, and visualization.
- Graylog: Another popular centralized logging and analytics tool that can work seamlessly with Spring Boot.
Monitoring and Maintenance
- Log Retention Policy: Decide on a policy depending on log significance, storage capabilities, and compliance requirements.
- Security: Ensure logging does not expose sensitive information inadvertently, especially with DEBUG or TRACE levels.
Proper logging practices enhance application monitoring, simplify troubleshooting, and improve system performance. Customizing and managing logs efficiently not only aids developers in debugging but also ensures maintainable and scalable applications in production.
Related reading
- Spring Boot logging with Lombok
- Spring Boot LoggingApplicationListener interfering with Application Server logging
- Spring boot multiple log files
- Spring Boot multiple SLF4J bindings
- Spring Boot Maven Plugin Include Resources
- Spring Boot MSSQL Kerberos Authentication
- Spring Boot requests to re-run your application with 'debug' enabled - how do I?
- Spring boot show sql parameter binding?

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.