Spring Boot
Logging
Java
Software Development
Application Configuration

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.

Practice system design

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:

  1. Logging output is sent to the console.
  2. The default log level is INFO.
  3. 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:

 
2023-10-03 10:00:00 INFO 12345 --- [ main] com.example.MyClass : Application started

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:

properties
1logging.level.root=INFO
2logging.level.com.example=DEBUG
3logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
4logging.file.name=application.log

For application.yml:

yaml
1logging:
2  level:
3    root: INFO
4    com.example: DEBUG
5  pattern:
6    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
7  file:
8    name: application.log

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:

xml
1<configuration>
2  <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"/>
3  
4  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
5    <encoder>
6      <pattern>${LOG_PATTERN}</pattern>
7    </encoder>
8  </appender>
9
10  <logger name="org.springframework" level="WARN"/>
11  <root level="INFO">
12    <appender-ref ref="CONSOLE"/>
13  </root>
14</configuration>

Best Practices for Spring Boot Logging

  • Use Appropriate Log Levels: Leverage different log levels to categorize messages. Avoid logging everything at INFO or ERROR levels.
  • 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

FeatureDefault Behavior/DescriptionCustomization Options
Logging FrameworkSLF4J & LogbackReplaceable with other frameworks like Log4j
Default OutputConsoleFile and other appenders
Default Log LevelINFOSet per package/class
Configuration FileN/Aapplication.properties/application.yml, logback-spring.xml
Asynchronous LoggingSynchronous by defaultConfigure AsyncAppender in Logback
Log PatternBasic date, level, messageCustomizable 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
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.