The Apache Log4j 1.x Compatibility API providing a bridge to Log4j 2.x implementations
—
The core logging functionality provides the primary interfaces for application logging through Logger, LogManager, Level, and Category classes.
public class Logger extends Category {
// Static factory methods
public static Logger getLogger(Class clazz);
public static Logger getLogger(String name);
public static Logger getRootLogger();
// Level checking methods
public boolean isTraceEnabled();
public boolean isDebugEnabled();
public boolean isInfoEnabled();
// Trace logging methods
public void trace(Object message);
public void trace(Object message, Throwable t);
}Parameters:
clazz - Class object used to determine logger namename - String name of the loggermessage - Log message object (toString() will be called)t - Throwable for stack trace loggingReturns:
Logger instance for the specified class or nameboolean indicating if the specified level is enabledpublic final class LogManager {
// Logger retrieval
public static Logger getLogger(Class clazz);
public static Logger getLogger(String name);
public static Logger getRootLogger();
public static Logger exists(String name);
// Logger enumeration
public static Enumeration getCurrentLoggers();
// Configuration management
public static void resetConfiguration();
public static void shutdown();
// Configuration constants
public static final String DEFAULT_CONFIGURATION_FILE;
public static final String DEFAULT_CONFIGURATION_KEY;
}Parameters:
clazz - Class object for logger name derivationname - String name of the logger to retrieve or checkReturns:
Logger instance or null if not found (exists method)Enumeration of current logger instancespublic class Category implements AppenderAttachable {
// Static factory methods
public static Category getInstance(String name);
public static Category getRoot();
// Level methods
public Level getLevel();
public void setLevel(Level level);
public Priority getPriority();
public void setPriority(Priority priority);
// Logging methods
public void debug(Object message);
public void debug(Object message, Throwable t);
public void info(Object message);
public void info(Object message, Throwable t);
public void warn(Object message);
public void warn(Object message, Throwable t);
public void error(Object message);
public void error(Object message, Throwable t);
public void fatal(Object message);
public void fatal(Object message, Throwable t);
public void log(Priority priority, Object message);
public void log(Priority priority, Object message, Throwable t);
// Level checking
public boolean isDebugEnabled();
public boolean isInfoEnabled();
public boolean isEnabledFor(Priority level);
// Appender management
public void addAppender(Appender appender);
public void removeAppender(Appender appender);
public void removeAllAppenders();
public Enumeration getAllAppenders();
public Appender getAppender(String name);
public boolean isAttached(Appender appender);
}Parameters:
name - String name for the categorylevel - Level object to setpriority - Priority object to setmessage - Log message objectt - Throwable for exception loggingappender - Appender to add/removeReturns:
Category instanceLevel or Priority objectboolean for level checks and appender operationsEnumeration of appendersAppender instance or nullpublic class Level extends Priority implements Serializable {
// 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 constants
public static final int TRACE_INT = 5000;
// Conversion methods
public static Level toLevel(String sArg);
public static Level toLevel(int val);
public static Level toLevel(String sArg, Level defaultLevel);
public static Level toLevel(int val, Level defaultLevel);
}Parameters:
sArg - String representation of level (e.g., "DEBUG", "INFO")val - Integer value of leveldefaultLevel - Default level to return if conversion failsReturns:
Level object corresponding to the inputpublic class Priority {
// Priority constants
public static final int OFF_INT;
public static final int FATAL_INT;
public static final int ERROR_INT;
public static final int WARN_INT;
public static final int INFO_INT;
public static final int DEBUG_INT;
public static final int ALL_INT;
// Legacy priority objects (deprecated)
public static final Priority FATAL;
public static final Priority ERROR;
public static final Priority WARN;
public static final Priority INFO;
public static final Priority DEBUG;
// Comparison methods
public boolean isGreaterOrEqual(Priority r);
// Conversion methods
public int toInt();
public String toString();
// Deprecated methods
@Deprecated
public static Priority toPriority(String sArg);
@Deprecated
public static Priority toPriority(int val);
}Parameters:
r - Priority object to compare againstsArg - String representation of priorityval - Integer value of priorityReturns:
boolean for comparison operationsint priority level valueString representation of priorityPriority object (deprecated methods)import org.apache.log4j.Logger;
public class MyService {
private static final Logger logger = Logger.getLogger(MyService.class);
public void processData(String data) {
logger.info("Processing data: " + data);
try {
// Process the data
if (logger.isDebugEnabled()) {
logger.debug("Data processed successfully");
}
} catch (Exception e) {
logger.error("Failed to process data", e);
throw e;
}
}
}import org.apache.log4j.Logger;
import org.apache.log4j.Level;
public class PerformanceService {
private static final Logger logger = Logger.getLogger(PerformanceService.class);
public void expensiveOperation() {
if (logger.isDebugEnabled()) {
String debugInfo = buildExpensiveDebugString();
logger.debug("Starting operation with: " + debugInfo);
}
// Check specific levels
if (logger.isEnabledFor(Level.TRACE)) {
logger.trace("Detailed trace information");
}
}
}import org.apache.log4j.Category;
import org.apache.log4j.Priority;
public class LegacyService {
private static final Category cat = Category.getInstance("LegacyService");
public void performTask() {
cat.log(Priority.INFO, "Task started");
try {
// Task implementation
cat.debug("Task in progress");
} catch (Exception e) {
cat.error("Task failed", e);
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-logging-log4j--log4j-1-2-api