Published on Sat May 03 2025 16:45:00 GMT+0000 (Coordinated Universal Time) by Purusothaman Ramanujam
Logging in Java: SLF4J and Logback Basics
Introduction
Logging is essential for understanding what your application is doing, diagnosing problems, and monitoring in production. In Java, SLF4J (Simple Logging Facade for Java) is the standard logging API, and Logback is a popular implementation that works seamlessly with SLF4J.
In this post, you’ll learn how to set up SLF4J and Logback, and use them for effective logging in your Java applications.
What are SLF4J and Logback?
- SLF4J is a logging facade that lets you plug in different logging frameworks (Logback, Log4j, java.util.logging, etc.)
- Logback is a modern, fast, and flexible logging framework, and the default implementation for SLF4J.
Adding SLF4J and Logback to Your Project
Maven Dependencies
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.4.11</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version></dependency>Gradle Dependencies
implementation 'ch.qos.logback:logback-classic:1.4.11'implementation 'org.slf4j:slf4j-api:2.0.9'Basic Usage
Creating a Logger
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
public class LoggingExample { private static final Logger logger = LoggerFactory.getLogger(LoggingExample.class);
public static void main(String[] args) { logger.info("Application started"); logger.debug("Debugging details"); logger.warn("A warning message"); logger.error("An error occurred"); }}info,debug,warn, anderrorare different log levels.
Logback Configuration
Logback is configured using an XML file named logback.xml in your src/main/resources directory. Here’s a simple example:
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT" /> </root></configuration>- This configures Logback to print logs to the console at the
infolevel or higher.
Logging Best Practices
- Use Placeholders: Avoid string concatenation in log messages.
logger.info("User {} logged in", username);
- Log at the Right Level: Use
debugfor development,infofor general events,warnfor potential issues, anderrorfor failures. - Don’t Log Sensitive Data: Avoid logging passwords or personal information.
- Externalize Configuration: Use
logback.xmlfor flexible log management. - Use Markers and MDC: For advanced logging, use Markers and Mapped Diagnostic Context (MDC) to add context to logs.
Conclusion
SLF4J and Logback make logging in Java simple and powerful. With a few dependencies and a basic configuration, you can add robust logging to your applications. Explore advanced features like rolling files, custom appenders, and MDC as you grow more comfortable.
Resources
Written by Purusothaman Ramanujam
← Back to blog