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

core-context.mddocs/

Core Context Management

The LoggerContext is the central anchor for the Log4j Core logging system, managing all loggers, configuration, and lifecycle operations. It provides the primary interface for obtaining loggers and managing configuration changes dynamically.

Capabilities

LoggerContext Management

Central management class for the entire logging system.

/**
 * Get the current LoggerContext for the calling thread
 * @return Current LoggerContext instance
 */
public static LoggerContext getContext();

/**
 * Get LoggerContext with current context flag
 * @param currentContext If true, returns context for current classloader
 * @return LoggerContext instance
 */
public static LoggerContext getContext(boolean currentContext);

/**
 * Get LoggerContext with specific parameters
 * @param loader ClassLoader to use for context selection
 * @param currentContext Whether to use current context
 * @param configLocation URI of configuration file
 * @return LoggerContext instance
 */
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation);

/**
 * Start the LoggerContext with current configuration
 */
public void start();

/**
 * Start the LoggerContext with specific configuration
 * @param config Configuration to use
 */
public void start(Configuration config);

/**
 * Stop the LoggerContext with timeout
 * @param timeout Maximum time to wait
 * @param timeUnit Time unit for timeout
 * @return true if stopped within timeout
 */
public boolean stop(long timeout, TimeUnit timeUnit);

Usage Examples:

import org.apache.logging.log4j.core.LoggerContext;
import java.net.URI;
import java.util.concurrent.TimeUnit;

// Get default context
LoggerContext context = LoggerContext.getContext();

// Get context with specific configuration
URI configURI = new URI("file:///path/to/log4j2.xml");
LoggerContext customContext = LoggerContext.getContext(
    MyClass.class.getClassLoader(), 
    false, 
    configURI
);

// Start and stop context
context.start();
boolean stopped = context.stop(30, TimeUnit.SECONDS);

Logger Management

Managing individual loggers within the context.

/**
 * Get logger by name
 * @param name Logger name
 * @return Logger instance
 */
public Logger getLogger(String name);

/**
 * Get logger with custom message factory
 * @param name Logger name  
 * @param messageFactory Message factory for the logger
 * @return Logger instance
 */
public Logger getLogger(String name, MessageFactory messageFactory);

/**
 * Get all loggers in this context
 * @return Collection of all loggers
 */
public Collection<Logger> getLoggers();

Usage Examples:

// Get specific loggers
Logger rootLogger = context.getLogger("");
Logger appLogger = context.getLogger("com.example.MyApp");
Logger serviceLogger = context.getLogger("com.example.service.MyService");

// Get all loggers
Collection<Logger> allLoggers = context.getLoggers();
System.out.println("Total loggers: " + allLoggers.size());

Configuration Management

Dynamic configuration management within the context.

/**
 * Get current configuration
 * @return Current Configuration instance
 */
public Configuration getConfiguration();

/**
 * Set new configuration
 * @param config New configuration to set
 * @return Previous configuration
 */
public Configuration setConfiguration(Configuration config);

/**
 * Reconfigure using existing configuration location
 */
public void reconfigure();

/**
 * Reconfigure with specific configuration
 * @param configuration Configuration to use for reconfiguration
 */
public void reconfigure(Configuration configuration);

Usage Examples:

// Get current configuration
Configuration currentConfig = context.getConfiguration();
System.out.println("Current config: " + currentConfig.getName());

// Reconfigure from file
context.reconfigure();

// Set new configuration
Configuration newConfig = new DefaultConfiguration();
Configuration oldConfig = context.setConfiguration(newConfig);

Filter Management

Global filter management for the context.

/**
 * Add a global filter to the context
 * @param filter Filter to add
 */
public void addFilter(Filter filter);

/**
 * Remove a global filter from the context
 * @param filter Filter to remove
 */
public void removeFilter(Filter filter);

Usage Examples:

import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.Level;

// Add global threshold filter
Filter thresholdFilter = ThresholdFilter.createFilter(Level.WARN, Filter.Result.ACCEPT, Filter.Result.DENY);
context.addFilter(thresholdFilter);

// Remove filter later
context.removeFilter(thresholdFilter);

Context State Management

Managing the lifecycle state of the context.

/**
 * Check if context is started
 * @return true if context is started
 */
public boolean isStarted();

/**
 * Check if context is stopped  
 * @return true if context is stopped
 */
public boolean isStopped();

/**
 * Get current lifecycle state
 * @return Current LifeCycle.State
 */
public LifeCycle.State getState();

Configurator Utility

Static utility class for common configuration operations.

/**
 * Initialize logger context with parameters
 * @param name Context name
 * @param loader ClassLoader to use
 * @param configLocation Configuration file location
 * @return Initialized LoggerContext
 */
public static LoggerContext initialize(String name, ClassLoader loader, String configLocation);

/**
 * Shutdown a logger context
 * @param context LoggerContext to shutdown
 */
public static void shutdown(LoggerContext context);

/**
 * Set level for specific logger
 * @param loggerName Logger name
 * @param level Level to set
 */
public static void setLevel(String loggerName, Level level);

/**
 * Set root logger level
 * @param level Level to set for root logger
 */
public static void setRootLevel(Level level);

/**
 * Set level for logger and all descendants
 * @param loggerName Logger name
 * @param level Level to set
 */
public static void setAllLevels(String loggerName, Level level);

Usage Examples:

import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.Level;

// Initialize with custom configuration
LoggerContext ctx = Configurator.initialize("MyApp", null, "log4j2-custom.xml");

// Set levels programmatically
Configurator.setRootLevel(Level.INFO);
Configurator.setLevel("com.example", Level.DEBUG);
Configurator.setLevel("com.example.verbose", Level.TRACE);

// Shutdown when done
Configurator.shutdown(ctx);

Install with Tessl CLI

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

docs

appenders.md

async-logging.md

configuration.md

core-context.md

filters.md

index.md

layouts.md

lookups.md

plugins.md

tile.json