The logging API of the Log4j project providing a comprehensive and flexible logging framework for Java applications.
—
Essential logging functionality providing the main Logger interface, LogManager factory, and logging levels. This is the primary API that most applications use for logging operations.
Central factory for obtaining Logger instances and managing the logging system lifecycle.
/**
* Central factory for obtaining Logger instances and managing LoggerContexts
*/
public final class LogManager {
/** Get logger for the calling class */
public static Logger getLogger();
/** Get logger for the specified class */
public static Logger getLogger(Class<?> clazz);
/** Get logger by name */
public static Logger getLogger(String name);
/** Get logger with custom message factory */
public static Logger getLogger(Class<?> clazz, MessageFactory messageFactory);
/** Get formatter logger for calling class (uses String.format) */
public static Logger getFormatterLogger();
/** Get formatter logger for specified class */
public static Logger getFormatterLogger(Class<?> clazz);
/** Get current LoggerContext */
public static LoggerContext getContext();
/** Get LoggerContext with control over context selection */
public static LoggerContext getContext(boolean currentContext);
/** Shutdown the logging system */
public static void shutdown();
/** Check if a logger with the given name exists */
public static boolean exists(String name);
/** Name of the root logger (empty string) */
public static final String ROOT_LOGGER_NAME = "";
}Usage Examples:
// Most common usage - logger for current class
private static final Logger logger = LogManager.getLogger();
// Logger for specific class
private static final Logger logger = LogManager.getLogger(MyClass.class);
// Logger by name
private static final Logger logger = LogManager.getLogger("com.example.module");
// Formatter logger (uses String.format instead of {} placeholders)
private static final Logger formatter = LogManager.getFormatterLogger();
formatter.info("User %s has %d items", username, itemCount);
// Application shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LogManager.shutdown();
}));Main logging interface providing methods for all logging levels and core logging operations.
/**
* Central interface for logging operations
*/
public interface Logger {
// Level-specific logging methods
void trace(String message);
void debug(String message);
void info(String message);
void warn(String message);
void error(String message);
void fatal(String message);
// Parameterized logging (all levels support Object... params)
void trace(String message, Object... params);
void debug(String message, Object... params);
void info(String message, Object... params);
void warn(String message, Object... params);
void error(String message, Object... params);
void fatal(String message, Object... params);
// Exception logging
void trace(String message, Throwable throwable);
void debug(String message, Throwable throwable);
void info(String message, Throwable throwable);
void warn(String message, Throwable throwable);
void error(String message, Throwable throwable);
void fatal(String message, Throwable throwable);
// Generic logging
void log(Level level, String message);
void log(Level level, String message, Object... params);
void log(Level level, String message, Throwable throwable);
// Level checking
boolean isTraceEnabled();
boolean isDebugEnabled();
boolean isInfoEnabled();
boolean isWarnEnabled();
boolean isErrorEnabled();
boolean isFatalEnabled();
boolean isEnabled(Level level);
// Flow tracing
void traceEntry();
void traceEntry(String format, Object... params);
<T> T traceExit(T result);
void traceExit();
// Exception flow
<T extends Throwable> T catching(T throwable);
<T extends Throwable> T throwing(T throwable);
// Properties
Level getLevel();
String getName();
MessageFactory getMessageFactory();
FlowMessageFactory getFlowMessageFactory();
}Usage Examples:
private static final Logger logger = LogManager.getLogger();
public void demonstrateLogging() {
// Basic logging
logger.info("Application started");
logger.warn("This is a warning message");
// Parameterized logging (efficient - no string concatenation if disabled)
String username = "alice";
int attempts = 3;
logger.info("User {} has {} login attempts", username, attempts);
// Exception logging
try {
riskyOperation();
} catch (Exception e) {
logger.error("Failed to perform risky operation", e);
}
// Conditional logging (avoid expensive operations)
if (logger.isDebugEnabled()) {
logger.debug("Expensive debug info: {}", expensiveCalculation());
}
// Generic level logging
Level dynamicLevel = getDynamicLevel();
logger.log(dynamicLevel, "Dynamic level message");
// Flow tracing
logger.traceEntry();
try {
String result = processData();
return logger.traceExit(result);
} catch (Exception e) {
throw logger.throwing(e);
}
}Hierarchy of logging levels with comparison capabilities for filtering and configuration.
/**
* Logging levels with hierarchy and comparison capabilities
*/
public final class Level implements Comparable<Level>, Serializable {
// Standard levels (in order of increasing verbosity)
public static final Level OFF; // No logging
public static final Level FATAL; // Fatal errors
public static final Level ERROR; // Error conditions
public static final Level WARN; // Warning conditions
public static final Level INFO; // Informational messages
public static final Level DEBUG; // Debug messages
public static final Level TRACE; // Trace messages
public static final Level ALL; // All messages
/** Get the numeric level value */
public int intLevel();
/** Check if this level is less specific than the given level */
public boolean isLessSpecificThan(Level level);
/** Check if this level is more specific than the given level */
public boolean isMoreSpecificThan(Level level);
/** Check if this level is within the given range */
public boolean isInRange(Level minLevel, Level maxLevel);
/** Create or get a custom level */
public static Level forName(String name, int intValue);
/** Get level by name (case-insensitive) */
public static Level valueOf(String name);
/** Convert string to level with fallback */
public static Level toLevel(String level);
public static Level toLevel(String level, Level defaultLevel);
@Override
public int compareTo(Level other);
}Usage Examples:
// Level comparison
Level currentLevel = logger.getLevel();
if (currentLevel.isMoreSpecificThan(Level.INFO)) {
// DEBUG or TRACE logging is enabled
logger.debug("Detailed debugging information");
}
// Custom levels
Level NOTICE = Level.forName("NOTICE", 350); // Between INFO(400) and WARN(300)
logger.log(NOTICE, "This is a notice");
// String to level conversion
String configLevel = System.getProperty("log.level", "INFO");
Level level = Level.toLevel(configLevel, Level.INFO);
// Level range checking
if (Level.DEBUG.isInRange(Level.TRACE, Level.INFO)) {
// DEBUG is between TRACE and INFO (inclusive)
}
// Sorting levels
List<Level> levels = Arrays.asList(Level.ERROR, Level.DEBUG, Level.INFO);
Collections.sort(levels); // Results in: DEBUG, INFO, ERRORSpecial logger instance representing the root of the logger hierarchy.
// Access root logger
Logger rootLogger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Logger rootLogger2 = LogManager.getLogger(""); // Same as above
// Root logger name constant
public static final String ROOT_LOGGER_NAME = "";Usage Examples:
// Configure root logger level programmatically (implementation-dependent)
Logger rootLogger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
// All loggers inherit from root logger's configuration
Logger specificLogger = LogManager.getLogger("com.example.MyClass");
// specificLogger will inherit root logger's level if not explicitly configuredInstall with Tessl CLI
npx tessl i tessl/maven-org-apache-logging-log4j--log4j-api