SLF4J 2 provider that bridges SLF4J 2 logging calls to the Apache Log4j API
npx @tessl/cli install tessl/maven-org-apache-logging-log4j--log4j-slf4j2-impl@2.25.0The Log4j SLF4J2 Implementation is a provider (binding) that bridges SLF4J 2.x logging calls to the Apache Log4j API. It enables applications using SLF4J 2.x to leverage Log4j 2 as the underlying logging implementation, providing access to advanced SLF4J 2 features like fluent logging API, structured data, and caller information detection while benefiting from Log4j 2's high-performance architecture and rich configuration capabilities.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.25.1</version>
</dependency>Gradle:
implementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.25.1'import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.MDC;
import org.slf4j.spi.LoggingEventBuilder;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public class MyApplication {
private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
public void doWork() {
// Basic logging
logger.info("Application starting");
logger.debug("Debug information: {}", someValue);
// Marker-based logging
Marker important = MarkerFactory.getMarker("IMPORTANT");
logger.warn(important, "Something significant happened");
// Fluent logging API (SLF4J 2.x)
logger.atInfo()
.addKeyValue("userId", userId)
.addKeyValue("operation", "login")
.log("User authentication successful");
}
}The implementation consists of several key components:
SLF4JServiceProvider automatically registers with SLF4J through Java ServiceLoader mechanismLog4jLoggerFactory creates and manages logger instances backed by Log4jLog4jLogger provides complete SLF4J Logger interface with Log4j backendLog4jMarkerFactory and Log4jMarker provide SLF4J marker support using Log4j markersLog4jMDCAdapter implements SLF4J MDC using Log4j's ThreadContextLog4jEventBuilder provides SLF4J 2.x fluent logging interfaceThrowableConsumingMessageFactory handles advanced message formatting with throwable extractionCore SLF4J logging functionality with all standard log levels (TRACE, DEBUG, INFO, WARN, ERROR) and parameterized messages.
// Standard logging methods for all levels
void trace(String msg);
void trace(String format, Object arg);
void trace(String format, Object arg1, Object arg2);
void trace(String format, Object... arguments);
void trace(String msg, Throwable t);
// Level checking methods
boolean isTraceEnabled();
boolean isDebugEnabled();
boolean isInfoEnabled();
boolean isWarnEnabled();
boolean isErrorEnabled();Enhanced logging with markers for categorization and filtering of log events.
// Marker-based logging methods (available for all levels)
void info(Marker marker, String msg);
void info(Marker marker, String format, Object arg);
void info(Marker marker, String format, Object arg1, Object arg2);
void info(Marker marker, String format, Object... arguments);
void info(Marker marker, String msg, Throwable t);
// Level checking with markers
boolean isInfoEnabled(Marker marker);SLF4J 2.x fluent interface for building complex log events with structured data and lazy evaluation.
LoggingEventBuilder atTrace();
LoggingEventBuilder atDebug();
LoggingEventBuilder atInfo();
LoggingEventBuilder atWarn();
LoggingEventBuilder atError();
interface LoggingEventBuilder {
LoggingEventBuilder setCause(Throwable cause);
LoggingEventBuilder addMarker(Marker marker);
LoggingEventBuilder addArgument(Object p);
LoggingEventBuilder addArgument(Supplier<?> objectSupplier);
LoggingEventBuilder addKeyValue(String key, Object value);
LoggingEventBuilder addKeyValue(String key, Supplier<Object> valueSupplier);
LoggingEventBuilder setMessage(String message);
LoggingEventBuilder setMessage(Supplier<String> messageSupplier);
void log();
void log(String message);
void log(String message, Object arg);
void log(String message, Object arg0, Object arg1);
void log(String message, Object... args);
void log(Supplier<String> messageSupplier);
}Thread-local context management for adding contextual information to log events.
// Basic MDC operations
void put(String key, String val);
String get(String key);
void remove(String key);
void clear();
Map<String, String> getCopyOfContextMap();
void setContextMap(Map<String, String> contextMap);
// Stack-based MDC operations
void pushByKey(String key, String value);
String popByKey(String key);
Deque<String> getCopyOfDequeByKey(String key);
void clearDequeByKey(String key);Creation and management of hierarchical markers for log event categorization.
Marker getMarker(String name);
boolean exists(String name);
boolean detachMarker(String name);
Marker getDetachedMarker(String name);
interface Marker {
String getName();
void add(Marker reference);
boolean remove(Marker reference);
boolean hasChildren();
boolean hasReferences();
Iterator<Marker> iterator();
boolean contains(Marker other);
boolean contains(String name);
}class SLF4JLoggingException extends RuntimeException {
SLF4JLoggingException(String msg);
SLF4JLoggingException(String msg, Exception ex);
SLF4JLoggingException(Exception ex);
}This implementation automatically integrates with SLF4J through the Java ServiceLoader mechanism. The SLF4JServiceProvider is automatically discovered and provides all required SLF4J factories and adapters.
class SLF4JServiceProvider implements org.slf4j.spi.SLF4JServiceProvider {
String REQUESTED_API_VERSION = "2.0.99";
ILoggerFactory getLoggerFactory();
IMarkerFactory getMarkerFactory();
MDCAdapter getMDCAdapter();
String getRequestedApiVersion();
void initialize();
}The implementation includes specialized message processing for compatibility with different logging frameworks:
class ThrowableConsumingMessageFactory implements MessageFactory2 {
Message newMessage(Object message);
Message newMessage(String message);
Message newMessage(String message, Object... params);
Message newMessage(String message, Object p0);
Message newMessage(String message, Object p0, Object p1);
// ... additional overloads for up to 10 parameters
}This factory implements Logback's algorithm for determining when the last parameter should be treated as a throwable rather than a message parameter.