Configuring Logback
Appenders, patterns, and levels.
What Is Logback?
Logback is a popular SLF4J backend, written by the same author as Log4j. It actually performs the logging that SLF4J calls describe.
You configure it with a file named logback.xml on the classpath, usually under src/main/resources.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
log.info("Logback reads logback.xml at startup");
}
}Three Building Blocks
Logback config has three core concepts:
- Logger: named source of log events, with a level.
- Appender: a destination (console, file).
- Encoder/Layout: formats the event into text.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
log.info("logger -> appender -> encoder -> output");
}
}All lessons in this course
- Why Structured Logging
- SLF4J Facade and Loggers
- Parameterized Logging
- Configuring Logback