CtrlK
BlogDocsLog inGet started
Tessl Logo

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

The Apache Log4j implementation of java.util.logging providing JUL to Log4j bridge functionality

Pending
Overview
Eval results
Files

logmanager-integration.mddocs/

LogManager Integration

The LogManager integration provides complete replacement of JUL's LogManager with Log4j's implementation, offering optimal performance by redirecting all JUL logging calls directly to Log4j without the overhead of handler-based bridging.

Setup

Enable LogManager integration by setting the system property:

System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");

Or via JVM arguments:

-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

Usage

Once configured, all standard JUL API calls are transparently redirected to Log4j:

// Standard JUL API - automatically uses Log4j
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("com.example.MyClass");

// All logging methods work as expected
logger.severe("Error message");
logger.warning("Warning message");  
logger.info("Info message");
logger.fine("Debug message");
logger.finest("Trace message");

// Parameterized messages
logger.log(java.util.logging.Level.INFO, "Processing {0} items", 42);

// Exception logging
try {
    // some operation
} catch (Exception e) {
    logger.log(java.util.logging.Level.SEVERE, "Operation failed", e);
}

Logger Adapter Selection

The LogManager automatically selects the most appropriate logger adapter:

  1. Custom Adapter: If log4j.jul.LoggerAdapter property is set
  2. CoreLoggerAdapter: If log4j-core is available (provides enhanced features)
  3. ApiLoggerAdapter: Fallback when only log4j-api is available
// Override adapter selection
System.setProperty("log4j.jul.LoggerAdapter", "com.example.CustomLoggerAdapter");

API Reference

LogManager Class

public class LogManager extends java.util.logging.LogManager {
    /**
     * Creates new LogManager instance with automatic adapter selection.
     * Selects CustomAdapter -> CoreLoggerAdapter -> ApiLoggerAdapter (fallback).
     */
    public LogManager();
    
    /**
     * Returns logger for the given name, creating if necessary.
     * Handles recursive calls and delegates to the selected adapter.
     * 
     * @param name the logger name
     * @return Logger instance backed by Log4j
     */
    public Logger getLogger(String name);
    
    /**
     * Returns enumeration of all logger names from the adapter context.
     * 
     * @return enumeration of logger names
     */
    public Enumeration<String> getLoggerNames();
    
    /**
     * Always returns false to prevent registration of non-bridged loggers.
     * All loggers must be obtained through getLogger(name).
     * 
     * @param logger the logger to add
     * @return always false
     */
    public boolean addLogger(Logger logger);
}

Configuration Properties

public final class Constants {
    /**
     * Property name for overriding the AbstractLoggerAdapter implementation.
     * Set to fully qualified class name with default constructor.
     */
    public static final String LOGGER_ADAPTOR_PROPERTY = "log4j.jul.LoggerAdapter";
}

Logger Implementations

The LogManager creates different logger implementations based on available Log4j components:

When log4j-core is Available

  • Uses CoreLoggerAdapter creating CoreLogger instances
  • Provides full JUL API support including level management
  • Enhanced performance and functionality

When Only log4j-api is Available

  • Uses ApiLoggerAdapter creating ApiLogger instances
  • Limited functionality due to API constraints
  • Methods like setLevel() and getParent() have limitations

Thread Safety

The LogManager implementation is fully thread-safe:

  • Uses ThreadLocal<Set<String>> to detect and prevent recursive logger creation
  • Returns NoOpLogger for recursive calls to prevent infinite loops
  • Adapter selection is performed once during construction

Performance Considerations

  • Optimal Performance: Direct delegation without handler overhead
  • Lazy Logger Creation: Loggers created on-demand through adapter
  • Context Isolation: Uses appropriate LoggerContext based on classloader
  • Recursive Call Prevention: Minimal overhead for recursive call detection

Error Handling

The LogManager gracefully handles various error conditions:

// Recursive calls are detected and logged
logger.warn("Recursive call to getLogger for {} ignored.", name);

// Custom adapter loading failures fall back to default
logger.error("Specified LoggerAdapter [{}] is incompatible.", className, exception);

Integration with Log4j Configuration

The LogManager respects all Log4j configuration settings:

  • Logger levels from log4j2.xml or log4j2.properties
  • Appender configurations
  • Filter configurations
  • Custom message factories and contexts

Migration Notes

When migrating from standard JUL to LogManager integration:

  1. No Code Changes: Existing JUL code works unchanged
  2. Configuration: Log4j configuration replaces JUL configuration
  3. Performance: Significant improvement over standard JUL
  4. Features: Access to advanced Log4j features like async logging
  5. Compatibility: Full compatibility with JUL API contracts

Install with Tessl CLI

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

docs

bridge-handler.md

index.md

level-translation.md

logger-adapters.md

logmanager-integration.md

tile.json