or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/maven-org-slf4j--slf4j-log4j12

SLF4J logging provider that integrates with reload4j logging framework, replacing legacy log4j 1.2 support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.slf4j/slf4j-log4j12@2.0.x

To install, run

npx @tessl/cli install tessl/maven-org-slf4j--slf4j-log4j12@2.0.0

index.mddocs/

SLF4J Reload4j Provider

A SLF4J logging provider that integrates the Simple Logging Facade for Java (SLF4J) with the reload4j logging framework. This provider enables applications using SLF4J to output logs through reload4j, providing a bridge from the legacy log4j 1.2 dependency to the actively maintained reload4j fork.

Package Information

  • Package Name: slf4j-log4j12 (relocated to slf4j-reload4j)
  • Package Type: Maven
  • Language: Java
  • Installation: <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>2.0.17</version></dependency>
  • Relocated to: <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId><version>2.0.17</version></dependency>

Core Imports

// Standard SLF4J imports - no direct imports of slf4j-reload4j classes needed
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

Basic Usage

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class ExampleApp {
    private static final Logger logger = LoggerFactory.getLogger(ExampleApp.class);
    
    public void processUser(String userId, String action) {
        // Add context information to MDC
        MDC.put("userId", userId);
        MDC.put("action", action);
        
        try {
            logger.info("Processing user action");
            
            // Business logic here
            if (action.equals("login")) {
                logger.debug("User login successful");
            } else if (action.equals("logout")) {
                logger.info("User logged out");
            }
            
        } catch (Exception e) {
            logger.error("Error processing user action", e);
        } finally {
            // Clean up MDC context
            MDC.clear();
        }
    }
}

Architecture

The SLF4J Reload4j Provider follows the SLF4J service provider pattern:

  • Service Provider: Reload4jServiceProvider implements SLF4JServiceProvider and is auto-discovered via Java ServiceLoader
  • Logger Factory: Reload4jLoggerFactory creates and caches logger instances, delegating to reload4j's LogManager
  • Logger Adapter: Reload4jLoggerAdapter wraps reload4j Logger instances and implements SLF4J Logger interface
  • MDC Adapter: Reload4jMDCAdapter bridges SLF4J MDC operations to reload4j's MDC implementation

The provider automatically initializes when SLF4J is used, requiring no manual configuration beyond adding the dependency.

Capabilities

Service Provider Implementation

Core SLF4J service provider that enables automatic discovery and initialization of the reload4j logging backend.

/**
 * Main SLF4J service provider for reload4j integration
 */
public class Reload4jServiceProvider implements SLF4JServiceProvider {
    /** SLF4J API version compatibility marker */
    public static String REQUESTED_API_VERSION = "2.0.99";
    
    /** Default constructor that initializes marker factory and MDC adapter */
    public Reload4jServiceProvider();
    
    /** Initialize the logger factory */
    public void initialize();
    
    /** Get the logger factory instance */
    public ILoggerFactory getLoggerFactory();
    
    /** Get the marker factory instance */
    public IMarkerFactory getMarkerFactory();
    
    /** Get the MDC adapter instance */
    public MDCAdapter getMDCAdapter();
    
    /** Get the requested API version */
    public String getRequestedApiVersion();
}

Logger Factory

Factory implementation that creates and manages logger instances, delegating to reload4j's LogManager.

/**
 * Logger factory that creates Reload4jLoggerAdapter instances
 */
public class Reload4jLoggerFactory implements ILoggerFactory {
    /** Default constructor that initializes logger cache and reload4j */
    public Reload4jLoggerFactory();
    
    /**
     * Get or create a logger for the specified name
     * @param name Logger name (class name or custom identifier)
     * @return Logger instance for the given name
     */
    public Logger getLogger(String name);
}

Logger Adapter

SLF4J Logger implementation that wraps reload4j Logger instances and provides full SLF4J API compatibility.

/**
 * SLF4J Logger implementation that delegates to reload4j Logger
 */
public final class Reload4jLoggerAdapter extends LegacyAbstractLogger 
    implements LocationAwareLogger, LoggingEventAware, Serializable {
    
    /** Check if TRACE level is enabled */
    public boolean isTraceEnabled();
    
    /** Check if DEBUG level is enabled */
    public boolean isDebugEnabled();
    
    /** Check if INFO level is enabled */
    public boolean isInfoEnabled();
    
    /** Check if WARN level is enabled */
    public boolean isWarnEnabled();
    
    /** Check if ERROR level is enabled */
    public boolean isErrorEnabled();
    
    /**
     * Location-aware logging method
     * @param marker Optional marker for categorization
     * @param callerFQCN Fully qualified class name of the caller
     * @param level Logging level as integer
     * @param msg Message to log
     * @param arguments Message formatting arguments
     * @param t Optional throwable for exception logging
     */
    public void log(Marker marker, String callerFQCN, int level, 
                   String msg, Object[] arguments, Throwable t);
    
    /**
     * Fluent API logging method for LoggingEvent
     * @param event LoggingEvent containing all log information
     */
    public void log(LoggingEvent event);
}

MDC Adapter

MDC (Mapped Diagnostic Context) adapter that bridges SLF4J MDC operations to reload4j's MDC implementation.

/**
 * MDC adapter that delegates to reload4j MDC implementation
 */
public class Reload4jMDCAdapter implements MDCAdapter {
    /** Clear all MDC values for current thread */
    public void clear();
    
    /**
     * Get MDC value for specified key
     * @param key The key to retrieve
     * @return String value or null if not found
     */
    public String get(String key);
    
    /**
     * Put key-value pair in MDC for current thread
     * @param key The key (cannot be null)
     * @param val The value (cannot be null for log4j compatibility)
     * @throws IllegalArgumentException if key or val is null
     */
    public void put(String key, String val);
    
    /**
     * Remove key from MDC for current thread
     * @param key The key to remove
     */
    public void remove(String key);
    
    /**
     * Get copy of current MDC context map
     * @return Map copy of current context, or null if no context
     */
    public Map getCopyOfContextMap();
    
    /**
     * Set MDC context map for current thread
     * @param contextMap Context map to set (null clears context)
     */
    public void setContextMap(Map<String, String> contextMap);
    
    /**
     * Push value to deque by key (SLF4J 2.0 feature)
     * @param key The key for the deque
     * @param value The value to push
     */
    public void pushByKey(String key, String value);
    
    /**
     * Pop value from deque by key (SLF4J 2.0 feature)  
     * @param key The key for the deque
     * @return The popped value or null if deque is empty
     */
    public String popByKey(String key);
    
    /**
     * Get copy of deque by key (SLF4J 2.0 feature)
     * @param key The key for the deque
     * @return Copy of the deque or null if not found
     */
    public Deque<String> getCopyOfDequeByKey(String key);
    
    /**
     * Clear deque by key (SLF4J 2.0 feature)
     * @param key The key for the deque to clear
     */
    public void clearDequeByKey(String key);
}

Service Provider Configuration

The provider is automatically discovered by SLF4J through the Java ServiceLoader mechanism:

Configuration File: META-INF/services/org.slf4j.spi.SLF4JServiceProvider
Content: org.slf4j.reload4j.Reload4jServiceProvider

No manual configuration is required - simply adding the dependency enables reload4j logging.

Dependencies

  • SLF4J API: Provides the core SLF4J interfaces (Logger, LoggerFactory, MDC, etc.)
  • Reload4j: The actual logging implementation framework (actively maintained log4j 1.2 fork)

Key Features

  • Automatic Discovery: Uses Java ServiceLoader for zero-configuration setup
  • Full SLF4J 2.0 Compatibility: Supports all SLF4J features including fluent API and advanced MDC operations
  • Thread Safety: All operations are thread-safe with proper synchronization
  • Performance Optimized: Efficient logger caching and delegation
  • MDC Support: Complete MDC implementation with both traditional and deque-based operations
  • Marker Support: Full marker support via SLF4J's BasicMarkerFactory
  • Location Awareness: Provides accurate source location information for debugging
  • Error Prevention: Built-in delegation loop detection prevents StackOverflowError

Error Handling

  • Version Compatibility Check: Validates reload4j version 1.2.12+ at initialization
  • Delegation Loop Prevention: Static checks prevent conflicts with log4j-over-slf4j
  • IllegalStateException: Thrown when delegation loops are detected
  • IllegalArgumentException: Thrown for invalid MDC operations (null keys/values)
  • NoSuchFieldError: Handled gracefully with informative error messages for version mismatches

Maven Relocation

As of version 2.0.17, the slf4j-log4j12 artifact has been relocated to slf4j-reload4j. When you depend on org.slf4j:slf4j-log4j12:2.0.17, Maven automatically resolves it to org.slf4j:slf4j-reload4j:2.0.17. This provides backward compatibility while transitioning to the actively maintained reload4j library.

To use the provider directly without relocation, depend on:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-reload4j</artifactId>
    <version>2.0.17</version>
</dependency>