SLF4J binding for Reload4j (Log4j 1.2.x successor) providing logging facade integration
npx @tessl/cli install tessl/maven-org-slf4j--slf4j-reload4j@1.7.0SLF4J Reload4j Binding provides a bridge between the SLF4J (Simple Logging Facade for Java) API and the Reload4j logging framework (successor to Log4j 1.2.x). This binding allows applications using SLF4J to delegate logging operations to Reload4j as the underlying logging implementation.
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId><version>1.7.36</version></dependency>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.ILoggerFactory;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.LocationAwareLogger;
import org.slf4j.event.LoggingEvent;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class Example {
private static final Logger logger = LoggerFactory.getLogger(Example.class);
public void demonstrateLogging() {
// Basic logging at different levels
logger.trace("Trace level message");
logger.debug("Debug level message");
logger.info("Info level message");
logger.warn("Warning message");
logger.error("Error message");
// Parameterized logging (avoids string concatenation)
String username = "alice";
int userId = 123;
logger.info("User {} logged in with ID {}", username, userId);
// Exception logging
try {
// some operation
} catch (Exception e) {
logger.error("Operation failed for user {}", username, e);
}
// MDC (Mapped Diagnostic Context) usage
MDC.put("requestId", "req-456");
logger.info("Processing request");
MDC.remove("requestId");
}
}The SLF4J Reload4j Binding operates through several key components:
Central factory for creating and managing logger instances with efficient caching.
public class StaticLoggerBinder implements LoggerFactoryBinder {
/**
* Returns the singleton instance of the logger binder.
* @return StaticLoggerBinder singleton
*/
public static final StaticLoggerBinder getSingleton();
/**
* SLF4J API version this binding was compiled against.
*/
public static String REQUESTED_API_VERSION;
/**
* Returns the logger factory instance.
* @return ILoggerFactory implementation
*/
public ILoggerFactory getLoggerFactory();
/**
* Returns the class name of the logger factory.
* @return String representation of factory class
*/
public String getLoggerFactoryClassStr();
}
public class Reload4jLoggerFactory implements ILoggerFactory {
/**
* Returns a logger instance for the given name.
* @param name Logger name (typically class name)
* @return Logger instance
*/
public Logger getLogger(String name);
}Complete logging interface supporting all standard levels with parameterized messages and exception handling.
public final class Reload4jLoggerAdapter extends MarkerIgnoringBase
implements LocationAwareLogger, Serializable {
// Level checking methods
public boolean isTraceEnabled();
public boolean isDebugEnabled();
public boolean isInfoEnabled();
public boolean isWarnEnabled();
public boolean isErrorEnabled();
// TRACE level logging
public void trace(String msg);
public void trace(String format, Object arg);
public void trace(String format, Object arg1, Object arg2);
public void trace(String format, Object... arguments);
public void trace(String msg, Throwable t);
// DEBUG level logging
public void debug(String msg);
public void debug(String format, Object arg);
public void debug(String format, Object arg1, Object arg2);
public void debug(String format, Object... arguments);
public void debug(String msg, Throwable t);
// INFO level logging
public void info(String msg);
public void info(String format, Object arg);
public void info(String format, Object arg1, Object arg2);
public void info(String format, Object... argArray);
public void info(String msg, Throwable t);
// WARN level logging
public void warn(String msg);
public void warn(String format, Object arg);
public void warn(String format, Object arg1, Object arg2);
public void warn(String format, Object... argArray);
public void warn(String msg, Throwable t);
// ERROR level logging
public void error(String msg);
public void error(String format, Object arg);
public void error(String format, Object arg1, Object arg2);
public void error(String format, Object... argArray);
public void error(String msg, Throwable t);
// Location-aware logging
public void log(Marker marker, String callerFQCN, int level, String msg, Object[] argArray, Throwable t);
public void log(LoggingEvent event);
}Thread-local storage for contextual information that appears in log messages.
public class StaticMDCBinder {
/**
* Singleton instance for MDC binding.
*/
public static final StaticMDCBinder SINGLETON;
/**
* Returns the singleton instance.
* @return StaticMDCBinder singleton
*/
public static final StaticMDCBinder getSingleton();
/**
* Returns the MDC adapter instance.
* @return MDCAdapter implementation
*/
public MDCAdapter getMDCA();
/**
* Returns the class name of the MDC adapter.
* @return String representation of MDC adapter class
*/
public String getMDCAdapterClassStr();
}
public class Reload4jMDCAdapter implements MDCAdapter {
/**
* Clear all entries in the MDC for the current thread.
*/
public void clear();
/**
* Get the context value for the given key.
* @param key Context key
* @return Context value or null if not found
*/
public String get(String key);
/**
* Put a context value for the given key.
* @param key Context key (cannot be null)
* @param val Context value (cannot be null for Log4j)
*/
public void put(String key, String val);
/**
* Remove the context value for the given key.
* @param key Context key to remove
*/
public void remove(String key);
/**
* Get a copy of the current thread's context map.
* @return Map containing all context entries, or null if empty
*/
public Map getCopyOfContextMap();
/**
* Set the current thread's context map.
* @param contextMap Map of context entries to set
*/
public void setContextMap(Map contextMap);
}Marker factory binding for structured logging markers.
public class StaticMarkerBinder implements MarkerFactoryBinder {
/**
* Singleton instance for marker binding.
*/
public static final StaticMarkerBinder SINGLETON;
/**
* Returns the singleton instance.
* @return StaticMarkerBinder singleton
*/
public static StaticMarkerBinder getSingleton();
/**
* Returns the marker factory instance.
* @return IMarkerFactory implementation (BasicMarkerFactory)
*/
public IMarkerFactory getMarkerFactory();
/**
* Returns the class name of the marker factory.
* @return String representation of marker factory class
*/
public String getMarkerFactoryClassStr();
}Core type definitions referenced in the API:
// From SLF4J API
interface ILoggerFactory {
Logger getLogger(String name);
}
interface MDCAdapter {
void clear();
String get(String key);
void put(String key, String val);
void remove(String key);
Map getCopyOfContextMap();
void setContextMap(Map contextMap);
}
interface LoggerFactoryBinder {
ILoggerFactory getLoggerFactory();
String getLoggerFactoryClassStr();
}
interface MarkerFactoryBinder {
IMarkerFactory getMarkerFactory();
String getMarkerFactoryClassStr();
}
interface LocationAwareLogger extends Logger {
int TRACE_INT = 0;
int DEBUG_INT = 10;
int INFO_INT = 20;
int WARN_INT = 30;
int ERROR_INT = 40;
void log(Marker marker, String fqcn, int level, String message, Object[] argArray, Throwable t);
}
interface LoggingEvent {
Level getLevel();
Marker getMarker();
String getLoggerName();
String getMessage();
String getThreadName();
Object[] getArgumentArray();
long getTimeStamp();
Throwable getThrowable();
}
enum Level {
ERROR, WARN, INFO, DEBUG, TRACE;
int toInt();
String toString();
}
// Standard Java types used
import java.util.Map;
import java.io.Serializable;// Efficient parameterized logging (recommended)
logger.debug("Processing user {} with {} items", userId, itemCount);
// Avoid string concatenation (inefficient)
// logger.debug("Processing user " + userId + " with " + itemCount + " items");
// Level checking for expensive operations
if (logger.isDebugEnabled()) {
String expensiveDebugInfo = computeExpensiveDebugInfo();
logger.debug("Debug info: {}", expensiveDebugInfo);
}try {
performDatabaseOperation();
} catch (SQLException e) {
// Log exception with context
logger.error("Database operation failed for user {}", userId, e);
}// Set context information
MDC.put("userId", String.valueOf(userId));
MDC.put("sessionId", sessionId);
try {
// All log messages in this thread will include MDC context
logger.info("Starting user operation");
performUserOperation();
logger.info("User operation completed successfully");
} finally {
// Clean up MDC to prevent memory leaks
MDC.clear();
}This binding requires the following dependencies:
The binding includes OSGi metadata and functions as a fragment bundle:
This binding delegates all configuration to the underlying Reload4j framework. Configure logging behavior through standard Reload4j configuration files (log4j.properties or log4j.xml).