Skip to content

Implement a Log4j API Provider #2

@ppkarwasz

Description

@ppkarwasz

Currently log4j2-android uses the log4j2.loggerContextFactory to register itself with the Log4j API. The usage of this property is deprecated since version 2.24.0 and will be removed in the future.

Please implement a Provider class and register it with ServiceLoader in a META-INF/services/org.apache.logging.log4j.spi.Provider class.

The Provider service determines both the LoggerContextFactory to use with the LogManager class and the ThreadContextMap to use with the ThreadContext class. Since the Android Log API does not handle context data, I would suggest to set the latter to NoOpThreadContextMap:

public class AndroidProvider extends Provider {
    private static final LoggerContextFactory CONTEXT_FACTORY = new AndroidLoggerContextFactory();

    public AndroidProvider() {
        // Since this provider is targeted at Android,
        // use a priority higher than those of the Log4j API implementations targeted at the JRE.
        super(25, CURRENT_VERSION);
    }

    @Override
    public LoggerContextFactory getLoggerContextFactory() {
        return CONTEXT_FACTORY;
    }

    @Override
    public ThreadContextMap getThreadContextMapInstance() {
        // Android does not provide an MDC implementation
        return NoOpThreadContextMap.INSTANCE;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions