Log4j 1.x API compatibility layer that implements Apache Log4j API over SLF4J for seamless migration
npx @tessl/cli install tessl/maven-org-slf4j--log4j-over-slf4j@2.0.0Log4j-over-SLF4J is a compatibility layer that implements the Apache Log4j 1.x API over SLF4J (Simple Logging Facade for Java). It allows applications originally written to use Log4j 1.x to seamlessly work with SLF4J-based logging implementations without requiring code changes. The library provides drop-in replacement classes for all major Log4j components, delegating their functionality to corresponding SLF4J components.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>2.0.17</version>
</dependency>import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.log4j.Level;For diagnostic contexts:
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;import org.apache.log4j.Logger;
import org.apache.log4j.Level;
// Get a logger instance
Logger logger = Logger.getLogger(MyClass.class);
// Or get by name
Logger namedLogger = Logger.getLogger("com.example.MyLogger");
// Log at different levels
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");
logger.fatal("Fatal message");
// Log with exceptions
try {
// some operation
} catch (Exception e) {
logger.error("Operation failed", e);
}
// Check if level is enabled
if (logger.isDebugEnabled()) {
logger.debug("Expensive debug operation: " + computeExpensiveData());
}Log4j-over-SLF4J is designed as a bridge between Log4j 1.x API and SLF4J implementations:
PropertyConfigurator are no-op since configuration is handled by SLF4JPrimary logging functionality including logger creation, level checking, and message logging at all standard Log4j levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL).
// Logger creation
public static Logger getLogger(String name);
public static Logger getLogger(Class<?> clazz);
public static Logger getRootLogger();
// Level checking
public boolean isDebugEnabled();
public boolean isInfoEnabled();
public boolean isWarnEnabled();
public boolean isErrorEnabled();
// Basic logging methods
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);Level and priority system providing standard Log4j levels (OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL) with conversion utilities and level comparison methods.
// Standard levels
public static final Level OFF;
public static final Level FATAL;
public static final Level ERROR;
public static final Level WARN;
public static final Level INFO;
public static final Level DEBUG;
public static final Level TRACE;
public static final Level ALL;
// Level conversion
public static Level toLevel(String sArg);
public static Level toLevel(int val);
public static Level toLevel(String sArg, Level defaultLevel);Mapped Diagnostic Context (MDC) and Nested Diagnostic Context (NDC) for adding contextual information to log messages across thread boundaries.
// MDC operations
public static void put(String key, String value);
public static Object get(String key);
public static void remove(String key);
public static void clear();
// NDC operations
public static void push(String message);
public static String pop();
public static String peek();
public static void clear();
public static int getDepth();Configuration utilities providing Log4j 1.x API compatibility for basic and property-based configuration (implemented as no-op since configuration is handled by SLF4J).
// Basic configuration
public static void configure();
public static void configure(Appender appender);
// Property configuration
public static void configure(Properties properties);
public static void configure(String configFilename);
public static void configureAndWatch(String configFilename);Appender interfaces and skeleton implementations for output destinations, plus layout classes for message formatting (mostly no-op implementations since output handling is managed by SLF4J).
// Appender interface
public interface Appender {
void doAppend(LoggingEvent event);
String getName();
void setName(String name);
boolean requiresLayout();
void close();
}
// Common appenders
public class ConsoleAppender extends WriterAppender;
public class FileAppender extends WriterAppender;
public class RollingFileAppender extends FileAppender;// Core logger hierarchy
public class Logger extends Category {
protected Logger(String name);
}
public class Category {
protected final String name;
protected org.slf4j.Logger slf4jLogger;
}
// Level and Priority
public class Level extends Priority implements Serializable {
protected Level(int level, String levelStr, int syslogEquivalent);
}
public class Priority {
transient int level;
transient String levelStr;
transient int syslogEquivalent;
}
// Service Provider Interfaces
public interface LoggerFactory {
public Logger makeNewLoggerInstance(String name);
}
public interface Appender {
void doAppend(LoggingEvent event);
String getName();
void setName(String name);
Layout getLayout();
void setLayout(Layout layout);
boolean requiresLayout();
void close();
}