or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

fluent-logging.mdindex.mdmarker-logging.mdmarker-management.mdmdc-support.mdstandard-logging.md
tile.json

tessl/maven-org-apache-logging-log4j--log4j-slf4j2-impl

SLF4J 2 provider that bridges SLF4J 2 logging calls to the Apache Log4j API

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.apache.logging.log4j/log4j-slf4j2-impl@2.25.x

To install, run

npx @tessl/cli install tessl/maven-org-apache-logging-log4j--log4j-slf4j2-impl@2.25.0

index.mddocs/

Log4j SLF4J2 Implementation

The 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.

Package Information

  • Package Name: org.apache.logging.log4j:log4j-slf4j2-impl
  • Package Type: maven
  • Language: Java
  • Installation: Add to Maven dependencies:
<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'

Core Imports

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;

Basic Usage

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");
    }
}

Architecture

The implementation consists of several key components:

  • Service Provider: SLF4JServiceProvider automatically registers with SLF4J through Java ServiceLoader mechanism
  • Logger Factory: Log4jLoggerFactory creates and manages logger instances backed by Log4j
  • Logger Implementation: Log4jLogger provides complete SLF4J Logger interface with Log4j backend
  • Marker System: Log4jMarkerFactory and Log4jMarker provide SLF4J marker support using Log4j markers
  • MDC Support: Log4jMDCAdapter implements SLF4J MDC using Log4j's ThreadContext
  • Fluent API: Log4jEventBuilder provides SLF4J 2.x fluent logging interface
  • Message Processing: ThrowableConsumingMessageFactory handles advanced message formatting with throwable extraction

Capabilities

Standard Logging

Core 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();

Standard Logging

Marker-Based Logging

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);

Marker-Based Logging

Fluent Logging API

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);
}

Fluent Logging API

MDC (Mapped Diagnostic Context)

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);

MDC Support

Marker Management

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);
}

Marker Management

Exception Types

class SLF4JLoggingException extends RuntimeException {
    SLF4JLoggingException(String msg);
    SLF4JLoggingException(String msg, Exception ex);
    SLF4JLoggingException(Exception ex);
}

Service Provider Integration

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();
}

Advanced Message Processing

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.