Spring Logging
Logback Setup
To use Logback as logging backend, we need to add the logback dependency in our pom.xml
and create a logback.xml
file in the resources folder. Once that is created, you can annotate your classes with @Slf4j
and log with log.<level>(Message);
pom.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.foo.bar" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
src/main/java/com/foo/ai/bar/Broker.java
@Slf4j
public class Broker {
log.info("Something happened.")
}