Adding SLF4J Logging to a Class
Fast recall
Quick revision
Core answer
org.slf4j.Logger and LoggerFactory, create a class-named logger, and write a parameterized event such as log.info("User {} created", userId). Spring Boot applications commonly already have SLF4J and a provider through a starter; a plain Java project must declare the SLF4J API and exactly one compatible runtime provider.Interview-ready guide
Interview answer
I import Logger and LoggerFactory, then declare private static final Logger log = LoggerFactory.getLogger(UserService.class). The class argument creates a stable package-based logger name. A first event can be log.info("User {} created", userId), using a placeholder instead of concatenation. In a typical Spring Boot application, an existing starter already brings SLF4J and the default Logback provider, but I verify the dependencies rather than assuming it. A plain Java application needs the SLF4J API and one compatible runtime provider. Lombok's @Slf4j is only a convenience that generates the same field.
Adding SLF4J Logging to a Class
Direct Answer
Add org.slf4j.Logger and LoggerFactory, create a class-named logger, and write a parameterized event such as log.info("User {} created", userId). Spring Boot applications commonly already have SLF4J and a provider through a starter; a plain Java project must declare the SLF4J API and exactly one compatible runtime provider.
Deep Dive & Architecture
Application code should depend on the SLF4J API. A typical class declares private static final Logger log = LoggerFactory.getLogger(UserService.class) and emits a parameterized event.
In a Spring Boot application, an ordinary starter usually supplies the SLF4J API and the default Logback provider. A minimal or deliberately reconfigured project may not, so check the dependency graph rather than assuming it.
A plain Java project needs slf4j-api plus one compatible provider at runtime. More than one provider can produce warnings and an unexpected selection; no provider means events are not handled as intended.
Code Example
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserService {
private static final Logger log = LoggerFactory.getLogger(UserService.class);
public User createUser(String email) {
log.info("Creating user with email={}", email); // your first log line
User user = repo.save(new User(email));
log.debug("Saved user id={}", user.id());
return user;
}
}
// Lombok shortcut — generates the same 'log' field automatically
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UserService {
public void demo() { log.info("log field generated by @Slf4j"); }
}
Common Mistakes & Pitfalls
- Wrong imports: importing a logging implementation instead of
org.slf4j.*. - Per-instance logger: dropping
static, creating a logger object per instance. - Concatenation:
log.info("email=" + email)instead oflog.info("email={}", email).
Check your understanding
Answer these without looking back at the article.
- What is SLF4J and why is it used instead of logging directly with a framework?
- How do you create and use a Logger in Java with SLF4J?
- How do you configure logging in a Spring Boot application?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.