The Apache Log4j 1.x Compatibility API providing a bridge to Log4j 2.x implementations
npx @tessl/cli install tessl/maven-org-apache-logging-log4j--log4j-1-2-api@2.25.0The Apache Log4j 1.x Compatibility API provides a seamless bridge between legacy Log4j 1.x applications and the modern Log4j 2.x infrastructure. This compatibility layer allows existing applications using Log4j 1.x syntax to work without code changes while benefiting from Log4j 2.x's improved performance, security, and features.
org.apache.logging.log4j:log4j-1.2-api2.25.1<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.25.1</version>
</dependency>implementation 'org.apache.logging.log4j:log4j-1.2-api:2.25.1'// Primary logging classes
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.log4j.Level;
// Configuration
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
// Appenders and layouts
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.PatternLayout;
// Thread context
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;import org.apache.log4j.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
public void doSomething() {
logger.info("Starting operation");
try {
// Your code here
logger.debug("Operation completed successfully");
} catch (Exception e) {
logger.error("Operation failed", e);
}
}
}import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
// Configure from properties file
PropertyConfigurator.configure("log4j.properties");
// Configure from XML file
DOMConfigurator.configure("log4j.xml");import org.apache.log4j.MDC;
import org.apache.log4j.NDC;
// Mapped Diagnostic Context
MDC.put("userId", "john123");
MDC.put("sessionId", "session456");
// Nested Diagnostic Context
NDC.push("WebService");
NDC.push("UserOperation");The Log4j 1.x Compatibility API consists of several key architectural components:
Primary logging interfaces including Logger, LogManager, and Level classes. Provides the main entry points for application logging with full Log4j 1.x compatibility.
Logger logger = Logger.getLogger(MyClass.class);
logger.info("Application started");
logger.error("Error occurred", exception);Configuration loading and management through PropertyConfigurator, DOMConfigurator, and programmatic configuration. Supports both traditional Log4j 1.x configuration formats.
PropertyConfigurator.configure("log4j.properties");
DOMConfigurator.configure("log4j.xml");
BasicConfigurator.configure();Output destinations including ConsoleAppender, FileAppender, RollingFileAppender, and custom appenders. Manages where log messages are written with full configurability.
ConsoleAppender appender = new ConsoleAppender(new PatternLayout("%d %p %c - %m%n"));
logger.addAppender(appender);Message formatting through PatternLayout, SimpleLayout, and custom layouts. Controls how log messages are formatted for output.
PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c{1} - %m%n");
SimpleLayout simpleLayout = new SimpleLayout();Thread-local diagnostic context through MDC (Mapped Diagnostic Context) and NDC (Nested Diagnostic Context). Enables context-aware logging across application threads.
MDC.put("userId", "user123");
MDC.put("requestId", "req456");
NDC.push("operation");Programmatic configuration builders that integrate with Log4j 2.x configuration system while maintaining Log4j 1.x compatibility.
AppenderBuilder<?> builder = new ConsoleAppenderBuilder<>();
Filter filter = new LevelRangeFilterBuilder().build();Service Provider Interface classes including LoggerRepository, ErrorHandler, Filter, and other extension points for custom functionality.
public interface ErrorHandler {
void error(String message, Exception ex, int errorCode);
void setAppender(Appender appender);
}