CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-logging-log4j--log4j-core

A versatile, industrial-grade, and reference implementation of the Log4j API with rich components for various logging use cases.

Pending
Overview
Eval results
Files

Apache Log4j Core

Apache Log4j Core is the reference implementation of the Log4j 2 logging framework, providing a comprehensive and industrial-grade logging solution for Java applications. It serves as the central logging engine that handles log event processing, routing, and output formatting across diverse environments with extensive configurability and high-performance capabilities.

Package Information

  • Package Name: log4j-core
  • Package Type: maven
  • Group ID: org.apache.logging.log4j
  • Language: Java
  • Installation: Add to Maven dependencies:
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.25.1</version>
    </dependency>

Core Imports

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;

For core implementation (advanced usage):

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.Filter;

Basic Usage

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;

// Get loggers (most common usage)
Logger logger = LogManager.getLogger(MyClass.class);
Logger rootLogger = LogManager.getRootLogger();
Logger namedLogger = LogManager.getLogger("com.example.MyLogger");

// Basic logging with different levels
logger.trace("Detailed trace information");
logger.debug("Debug information: {}", debugValue);
logger.info("Application started successfully");
logger.warn("This is a warning message");
logger.error("Error occurred: {}", errorMessage, exception);
logger.fatal("Critical system failure", criticalException);

// Using markers for categorization
Marker sqlMarker = MarkerManager.getMarker("SQL");
Marker securityMarker = MarkerManager.getMarker("SECURITY");
logger.info(sqlMarker, "Database query executed: {}", query);
logger.warn(securityMarker, "Failed login attempt from {}", ipAddress);

// Thread context (MDC/NDC) for request tracking
ThreadContext.put("userId", "12345");
ThreadContext.put("sessionId", "abc-def-ghi");
ThreadContext.push("operation-name");
logger.info("Processing user request");
ThreadContext.clearAll();

// Programmatic configuration (advanced)
import org.apache.logging.log4j.core.config.Configurator;
Configurator.setRootLevel(Level.INFO);
Configurator.setLevel("com.example", Level.DEBUG);

Architecture

Apache Log4j Core is built around several key architectural components:

  • LoggerContext: Central management hub for all loggers and configuration
  • Configuration System: Multi-format configuration support (XML, JSON, YAML, Properties)
  • Appender Framework: Output destination system with 15+ built-in appenders
  • Layout System: Message formatting with 10+ built-in layouts
  • Filter Chain: Fine-grained event filtering with 15+ built-in filters
  • Plugin Architecture: Extensible system for custom components with annotation-based registration
  • Async Framework: High-performance asynchronous logging using LMAX Disruptor
  • Builder API: Fluent programmatic configuration interface

Capabilities

Logger Management

Central entry point for obtaining and managing loggers throughout the application.

/**
 * LogManager - Static factory for obtaining loggers
 */
public static Logger getLogger();
public static Logger getLogger(Class<?> clazz);
public static Logger getLogger(String name);
public static Logger getRootLogger();
public static LoggerContext getContext();
public static LoggerContext getContext(boolean currentContext);
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation);

/**
 * Logger - Primary logging interface for applications  
 */
public interface Logger {
    // Basic logging methods for all levels
    void trace(String message);
    void trace(String message, Object... params);
    void trace(String message, Throwable throwable);
    void trace(Marker marker, String message);
    void trace(Marker marker, String message, Object... params);
    void trace(Marker marker, String message, Throwable throwable);
    
    void debug(String message);
    void debug(String message, Object... params); 
    void debug(String message, Throwable throwable);
    void debug(Marker marker, String message);
    void debug(Marker marker, String message, Object... params);
    void debug(Marker marker, String message, Throwable throwable);
    
    void info(String message);
    void info(String message, Object... params);
    void info(String message, Throwable throwable);
    void info(Marker marker, String message);
    void info(Marker marker, String message, Object... params);
    void info(Marker marker, String message, Throwable throwable);
    
    void warn(String message);
    void warn(String message, Object... params);
    void warn(String message, Throwable throwable);
    void warn(Marker marker, String message);
    void warn(Marker marker, String message, Object... params);
    void warn(Marker marker, String message, Throwable throwable);
    
    void error(String message);
    void error(String message, Object... params);
    void error(String message, Throwable throwable);
    void error(Marker marker, String message);
    void error(Marker marker, String message, Object... params);
    void error(Marker marker, String message, Throwable throwable);
    
    void fatal(String message);
    void fatal(String message, Object... params);
    void fatal(String message, Throwable throwable);
    void fatal(Marker marker, String message);
    void fatal(Marker marker, String message, Object... params);
    void fatal(Marker marker, String message, Throwable throwable);
    
    // Level checking methods
    boolean isTraceEnabled();
    boolean isDebugEnabled();
    boolean isInfoEnabled();
    boolean isWarnEnabled();
    boolean isErrorEnabled();
    boolean isFatalEnabled();
    boolean isEnabled(Level level);
    boolean isEnabled(Level level, Marker marker);
    
    // Logger properties
    String getName();
    Level getLevel();
}

Thread Context Management

Thread-local context management for Mapped Diagnostic Context (MDC) and Nested Diagnostic Context (NDC).

/**
 * ThreadContext - Thread-local diagnostic context management
 */
public static void put(String key, String value);
public static String get(String key);
public static void remove(String key);
public static void removeAll(Iterable<String> keys);
public static void clearMap();
public static void clearAll();
public static boolean containsKey(String key);
public static Map<String, String> getContext();
public static Map<String, String> getImmutableContext();
public static boolean isEmpty();

// Nested Diagnostic Context (NDC) operations
public static void push(String message);
public static void push(String message, Object... args);
public static String pop();
public static String peek();
public static void trim(int maxDepth);
public static ContextStack getImmutableStack();
public static int getDepth();
public static void clearStack();

Marker System

Hierarchical markers for categorizing and filtering log events.

/**
 * MarkerManager - Factory for creating and managing markers
 */
public static Marker getMarker(String name);
public static Marker getMarker(String name, Marker parent);
public static Marker getMarker(String name, Marker... parents);
public static boolean exists(String name);
public static void clear();

/**
 * Marker - Hierarchical event categorization
 */
public interface Marker {
    String getName();
    Marker[] getParents();
    boolean hasParents();
    boolean isInstanceOf(Marker marker);
    boolean isInstanceOf(String name);
    Marker addParents(Marker... markers);
    boolean remove(Marker marker);
    Marker setParents(Marker... markers);
}

Message System

Message creation and formatting system for parameterized and structured logging.

/**
 * MessageFactory - Factory for creating Message instances
 */
public interface MessageFactory {
    Message newMessage(Object message);
    Message newMessage(String message);
    Message newMessage(String message, Object... params);
}

/**
 * Message - Base interface for all log messages
 */
public interface Message {
    String getFormattedMessage();
    String getFormat();
    Object[] getParameters();
    Throwable getThrowable();
}

/**
 * ParameterizedMessage - Efficient parameterized message implementation
 */
public class ParameterizedMessage implements Message {
    public ParameterizedMessage(String messagePattern, Object[] arguments);
    public ParameterizedMessage(String messagePattern, Object... arguments);
    public ParameterizedMessage(String messagePattern, Object arg);
    public ParameterizedMessage(String messagePattern, Object arg1, Object arg2);
    // Additional constructors for performance optimization
}

/**
 * FormattedMessage - Printf-style formatted messages
 */
public class FormattedMessage implements Message {
    public FormattedMessage(String messagePattern, Object... arguments);
    public FormattedMessage(Locale locale, String messagePattern, Object... arguments);
}

/**
 * ObjectMessage - Simple object-based messages
 */
public class ObjectMessage implements Message {
    public ObjectMessage(Object obj);
}

Core Logging Context

Central management of loggers, configuration, and lifecycle operations. Essential for any logging setup and dynamic configuration changes.

public static LoggerContext getContext();
public static LoggerContext getContext(boolean currentContext);
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation);

public void start();
public void start(Configuration config);
public boolean stop(long timeout, TimeUnit timeUnit);
public Logger getLogger(String name);
public Configuration getConfiguration();
public Configuration setConfiguration(Configuration config);

Core Context Management

Appender System

Output destination framework for routing log events to various targets including files, databases, network sockets, and message queues.

public interface Appender extends LifeCycle {
    void append(LogEvent event);
    String getName();
    Layout<? extends Serializable> getLayout();
    boolean ignoreExceptions();
    ErrorHandler getHandler();
}

Appenders

Layout System

Message formatting system that converts log events into structured output formats including pattern-based, JSON, XML, and custom formats.

public interface Layout<T extends Serializable> extends Encodable {
    byte[] getHeader();
    byte[] getFooter();
    byte[] toByteArray(LogEvent event);
    T toSerializable(LogEvent event);
    String getContentType();
}

Layouts

Filter Framework

Event filtering system for fine-grained control over which log events are processed, with support for level-based, marker-based, and custom filtering.

public interface Filter {
    enum Result { ACCEPT, NEUTRAL, DENY }
    
    Result getOnMatch();
    Result getOnMismatch();
    Result filter(Logger logger, Level level, Marker marker, String msg, Object... params);
    Result filter(LogEvent event);
}

Filters

Configuration Management

Multi-format configuration system supporting XML, JSON, YAML, and Properties files, plus programmatic configuration through builder API.

public interface Configuration {
    String getName();
    LoggerConfig getLoggerConfig(String name);
    Map<String, Appender> getAppenders();
    LoggerConfig getRootLogger();
    List<String> getPluginPackages();
}

Configuration

Variable Substitution Lookups

Dynamic variable substitution system for configuration files, supporting environment variables, system properties, context data, and custom lookup implementations.

/**
 * Core lookup interface for variable substitution
 */
public interface StrLookup {
    String lookup(String key);
    String lookup(LogEvent event, String key);
}

// Built-in lookup implementations
public class SystemPropertiesLookup implements StrLookup;
public class EnvironmentLookup implements StrLookup;
public class ContextMapLookup implements StrLookup;
public class DateLookup implements StrLookup;
public class JavaLookup implements StrLookup;

Variable Lookups

Asynchronous Logging

High-performance asynchronous logging system using LMAX Disruptor for maximum throughput with minimal latency impact on application threads.

public class AsyncLogger extends Logger {
    // Inherits all Logger methods with async implementation
}

public interface AsyncQueueFullPolicy {
    EventRoute getRoute(long backgroundThreadId, Level level);
}

Async Logging

Plugin Architecture

Extensible plugin system allowing custom appenders, layouts, filters, and other components through annotation-based registration and factory methods.

@Plugin(name = "MyPlugin", category = Core.CATEGORY_NAME, elementType = "appender")
public class MyAppender extends AbstractAppender {
    @PluginFactory
    public static MyAppender createAppender(
        @PluginAttribute("name") String name,
        @PluginElement("Layout") Layout<? extends Serializable> layout) {
        // Factory implementation
    }
}

Plugin System

Types

public enum Level {
    OFF(0), FATAL(100), ERROR(200), WARN(300), INFO(400), DEBUG(500), TRACE(600), ALL(Integer.MAX_VALUE);
    
    public boolean isMoreSpecificThan(Level level);
    public boolean isLessSpecificThan(Level level);
}

public interface LogEvent {
    Level getLevel();
    String getLoggerName();
    Message getMessage();
    long getTimeMillis();
    Marker getMarker();
    String getThreadName();
    StackTraceElement getSource();
    Throwable getThrown();
}

public interface LifeCycle {
    enum State { INITIALIZING, INITIALIZED, STARTING, STARTED, STOPPING, STOPPED }
    
    State getState();
    void initialize();
    void start();
    void stop();
    boolean isStarted();
    boolean isStopped();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-logging-log4j--log4j-core
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.apache.logging.log4j/log4j-core@2.25.x
Badge
tessl/maven-org-apache-logging-log4j--log4j-core badge