Sub topic 9
Configuring Logback
This chapter assumes basic Java, Maven or Gradle dependency management, and familiarity with SLF4J logging calls. You will configure Logback as the runtime provider for SLF4J, choose output formatting, and set different levels for different packages.
Add Logback as the SLF4J provider
SLF4J is a logging facade. Your application compiles against slf4j-api, while a provider performs the actual logging at runtime. Logback is one such provider.
For a Maven project, add the API as a normal dependency and Logback as a runtime dependency:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.12</version>
<scope>runtime</scope>
</dependency>
</dependencies>logback-classic includes the Logback implementation and the SLF4J provider. The versions must be compatible. A provider on the runtime classpath is required even though application source code imports only SLF4J classes.
Create a Logback configuration
Logback searches the classpath for logback.xml. Put this file in src/main/resources, so Maven copies it into the application classpath.
<configuration>
<appender name="CONSOLE"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.example.payments" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>An appender defines where events go. ConsoleAppender writes to the console. The encoder converts each event into text. This pattern includes the timestamp, level, thread, abbreviated logger name, message, and newline.
A logger level controls which events are accepted. The root logger is the fallback for every logger. Here, the application logs at INFO or higher by default, while the com.example.payments package also accepts DEBUG.
Log levels are ordered from most detailed to most severe:
| Level | Typical use |
|---|---|
| `DEBUG` | Diagnostic details useful during investigation |
| `INFO` | Normal application milestones |
| `WARN` | An unusual condition that did not stop the operation |
| `ERROR` | A failed operation or unexpected failure |
TRACE is more detailed than DEBUG. Logback also recognizes OFF and ALL for configuration purposes. A logger inherits its level and appenders from its nearest configured parent unless you configure it separately.
Understand logger names and inheritance
SLF4J normally names a logger after its class:
package com.example.payments;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class PaymentService {
private static final Logger log =
LoggerFactory.getLogger(PaymentService.class);
public void charge(String customerId, long amountCents) {
log.info("Charging customer {} amount {} cents",
customerId, amountCents);
if (amountCents <= 0) {
log.warn("Rejected non-positive charge for customer {}",
customerId);
}
}
}The logger name is com.example.payments.PaymentService. It inherits the DEBUG level from com.example.payments, so a log.debug(...) call in this class is enabled. A class in com.example.web instead inherits the root INFO level.
If you configure an appender on both a package logger and the root logger, events can be written twice because appenders are cumulative by default. Use additivity="false" when a logger should stop propagation:
<logger name="com.example.audit"
level="INFO"
additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>Trace the configuration flow
If SLF4J reports that no provider was found, logback-classic is missing from the runtime classpath, is excluded by dependency resolution, or is incompatible with the SLF4J API. The application may still run, but logging will use a fallback and will not follow your Logback configuration.
Exercise
Exercise
The root logger is set to INFO. You need DEBUG output only for classes under com.example.orders. Which logger element should you add?
Show answer
Add <logger name="com.example.orders" level="DEBUG"/>. Child package and class loggers inherit this level, while other loggers continue to inherit INFO from the root.