SLF4J Logging API

SLF4J is a logging facade that lets Java application code use a stable logging API without depending directly on a particular logging implementation. A concrete backend such as Logback or Log4j2 performs the actual logging work.

SLF4J separates logging calls from the logging engine. Application code typically obtains an SLF4J `Logger` and calls methods such as `debug`, `info`, `warn`, and `error`, while the chosen backend handles formatting, filtering, destinations, and other implementation details.

This abstraction makes it easier to change logging implementations without rewriting application classes. The application depends on SLF4J's API, while the runtime configuration connects that API to a compatible provider.

Parameterized logging lets values be supplied separately from the message template, for example `logger.info("User {} logged in", userId)`. This avoids constructing the final message when the corresponding log level is disabled and is generally preferable to string concatenation.

Log levels represent increasing severity or importance, commonly including `TRACE`, `DEBUG`, `INFO`, `WARN`, and `ERROR`. Which messages are emitted depends on the configured logging backend and its level configuration.

Key points

Interview questions

What is the difference between SLF4J and a logging implementation?

SLF4J supplies the API that application code calls, such as `Logger.info()` and `Logger.error()`. The implementation or provider supplies the behavior behind those calls, including writing records to configured destinations and applying implementation-specific configuration. This allows the application to depend on the facade while the runtime uses a selected backend.

Which logging level would you use for an unexpected exception?

If the exception represents a failure that needs attention, `ERROR` is commonly appropriate. If the condition is expected and handled as part of normal operation, a lower level such as `INFO` or `WARN` may be more suitable depending on the context. The important consideration is the operational meaning of the event, not simply whether an exception object exists.

What is SLF4J, and why would you use it?

SLF4J is a logging facade for Java applications. It provides a common API while allowing the application to remain independent of a particular logging implementation. A backend such as Logback or Log4j2 can handle the actual logging. This separation makes backend changes less invasive.

Why is `logger.info("User {}", id)` preferable to `logger.info("User " + id)`?

The placeholder form uses parameterized logging. The logger receives the template and argument separately, so unnecessary message construction can be avoided when that level is disabled. It also keeps logging statements concise and consistent.

How does SLF4J help when a project changes logging backends?

Application classes can depend on the stable SLF4J API instead of directly calling backend-specific APIs. A compatible provider can then be selected and configured separately. This reduces coupling and limits backend-specific code to configuration or integration boundaries. The exact migration work still depends on the backend and APIs used by the application.