Logback is a powerful and flexible logging framework for Java applications. It is designed to be faster and have a smaller memory footprint compared to its predecessor, Log4j. Logback is the default logging framework used in Spring Boot, making it an excellent choice for enterprise applications due to its simplicity and performance.
Why Use Logback with Spring Boot?
Spring Boot simplifies the setup of Logback by providing sensible defaults and auto-configuration. This means you can start using Logback without needing to add any extra libraries or perform complex configurations. Spring Boot versions 2 and 3 include Logback as part of the spring-boot-starter-logging
dependency, which is included in most Spring Boot starter dependencies.
Getting Started with Logback in Spring Boot
- Initial Setup:
- Create a new Spring Boot project using Spring Initializr or your preferred method.
- Ensure that you have the
spring-boot-starter-web
dependency in yourpom.xml
orbuild.gradle
file. This starter includesspring-boot-starter-logging
, which in turn includes Logback.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Basic Configuration:
- By default, Spring Boot uses Logback for logging. You don’t need to add any additional dependencies.
- You can customize Logback’s configuration by creating a
logback.xml
file in thesrc/main/resources
directory.
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT" /> </root> </configuration>
Using Logback in Your Application:
- You can use SLF4J (Simple Logging Facade for Java) to log messages in your Spring Boot application. SLF4J is a facade for various logging frameworks, including Logback.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class LoggingController { private static final Logger logger = LoggerFactory.getLogger(LoggingController.class); @GetMapping("/") public String index() { logger.info("An INFO message"); logger.warn("A WARN message"); logger.error("An ERROR message"); return "Check the logs for output!"; } }
An excellent introduction to SLFJ is available in this article: How to configure SLF4J in WildFly applications
Advanced LogBack Configuration
The following one, is a more advanced configuration which includes a RollingFileAppender and shows how to use properties inside your configuration:
<configuration> <property name="LOG_ROOT" value="c:/temp/logs" /> <property name="LOG_FILE_NAME" value="application" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_ROOT}/${LOG_FILE_NAME}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_ROOT}/${LOG_FILE_NAME}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <!-- each archived file's size will be max 10MB --> <maxFileSize>10MB</maxFileSize> <!-- 30 days to keep --> <maxHistory>30</maxHistory> <!-- total size of all archive files, if total size > 100GB, it will delete old archived file --> <totalSizeCap>100GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="com.example.demo" level="INFO" additivity="false"> <appender-ref ref="FILE"/> </logger> <root level="ERROR"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
Conclusion
Configuring Logback with Spring Boot is straightforward and does not require any additional libraries beyond what is already included in the Spring Boot starters. With sensible defaults and easy customization options, Logback provides a robust logging solution for your Spring Boot applications12.
Found the article helpful? if so please follow us on Socials