Search code examples
javalog4jquarkus

Quarkus, Log4J 1-2 Bridge, and Uber Jar


I have this code, which initializes a Log4J context in my application. The application is being developed using Quarkus 2.16.7.Final. Log4j context is wrapped inside my generic "loggingContext" class, which exposes "initialize" method to use for logging context initialization inside a more generic application context:

public synchronized void initialize(ApplicationContext currentApplicationContext) throws LoggingContextException {
    if (!isInitialized()) {
        File configurationFile = null;
        configurationFile = new File(getConfigurationContext().getLog4jFilePath());
        if (!configurationFile.exists()) {
            throw new LoggingContextException("Could not find LOG4J configuration file: \"" + getConfigurationContext().getLog4jFilePath() + "\".");
        }
        this.loggingId = currentApplicationContext.getApplicationName();
        try (FileInputStream fis = new FileInputStream(configurationFile);) {
            this.loggerContext = LoggerContext.getContext(getConfigurationContext().getClassLoader(), false, null);
            loggerContextOwnership = initLoggerContext(this.loggerContext, configurationFile);
            if (loggerContextOwnership) {
                System.out.println(loggingId + " - Logging Context INITIALIZED (LOG4J File Path: \"" + this.loggerContext.getConfiguration().getConfigurationSource().getFile() + "\"). <<<");
            } else {
                File configFile = loggerContext.getConfiguration().getConfigurationSource().getFile();
                if (!configFile.getAbsolutePath().equals(configurationFile.getAbsolutePath())) {
                    throw new LoggingContextException("Could not acquire LOG4J context ownership.");
                }
            }
        } catch (IOException e) {
            throw new LoggingContextException("Could not read LOG4J configuration file", e);
        }
        this.initialized = true;
        super.initialize(currentApplicationContext);
    }
}
private boolean initLoggerContext(LoggerContext loggerContext, File log4jConfigurationFile) {
    boolean hasOwnership = false;
    Configuration xmlConfig = ConfigurationFactory.getInstance().getConfiguration(null, "Application Framework LOG4J configuration", log4jConfigurationFile.toURI());
    File configFile = loggerContext.getConfiguration().getConfigurationSource().getFile();
    String key = getConfigurationContext().getLog4jFilePath();
    if (configFile == null) {
        loggerContext.setConfiguration(xmlConfig);
        hasOwnership = true;
        synchronized (log4jContextMap) {
            List<LOG4JContext> list = log4jContextMap.get(getConfigurationContext().getClassLoader()).computeIfAbsent(key, k -> {
                return new LinkedList<>();
            });
            list.add(this);
        }
    } else {
        if (!configFile.getAbsolutePath().equals(log4jConfigurationFile.getAbsolutePath())) {
            System.out.println(loggingId + " - WARNING: LOG4J Context already initialized (file: \"" + configFile.getAbsolutePath() + "\").");
            LoggingOwnershipMode loggingOwnershipMode = getConfigurationContext().getLoggingOwnershipMode();
            switch (loggingOwnershipMode) {
                case DEFAULT:
                    break;
                case FORCE:
                    loggerContext.setConfiguration(xmlConfig);
                    hasOwnership = true;
                    System.out.println(loggingId + " - WARNING: LOG4j Context has been reconfigured.");
                    break;
                default:
                    break;
            }
        } else {
            hasOwnership = true;
            synchronized (log4jContextMap) {
                List<LOG4JContext> list = log4jContextMap.get(getConfigurationContext().getClassLoader()).computeIfAbsent(key, k -> {
                    return new LinkedList<>();
                });
                list.add(this);
            }
        }
    }
    return hasOwnership;
}

I have these dependencies (dependency hierarchy, Eclipse IDE view):

Quarkus JAR Dependencies

My Log4J context works (on Windows) only with FAST-JAR configuration. I would like to use a Uber-Jar instead. Using an Uber Jar I have the following SystemOut:

SystemOut

I do not want to use Jboss logging since this application is a porting from a J2EE application which heavily uses log4j. Using fast jar is not accepted as final solution. I am pretty sure the problem is related to classloading mechanisms, but I would like to overcome it. Any suggestions?

Note

I am not sure this is related with the proposed duplicate question. We are creating a Uber-Jar in Quarkus semantics. maven-shade-plugin is not used and the question is related to adding log4j1-2 bridge to Quarkus which works in fast-jar mode but not uber-jar/legacy-jar.


Solution

  • Problem was solved by removing from the final runner.jar the file "Log4j2Plugins.dat". this file should be merged instead of removed but i just need log4j2-CORE plugins which should be annotated with @Plugin and they are enough for me.

    The warning of Quarkus is enough to understand that the file is the problem (packaging uber-jar):

    [WARNING] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Dependencies with duplicate files detected. The dependencies [org.apache.logging.log4j:log4j-1.2-api::jar:2.19.0[paths: H:\Maven\org\apache\logging\log4j\log4j-1.2-api\2.19.0\log4j-1.2-api-2.19.0.jar;], org.apache.logging.log4j:log4j-core::jar:2.19.0[paths: H:\Maven\org\apache\logging\log4j\log4j-core\2.19.0\log4j-core-2.19.0.jar;], org.apache.logging.log4j:log4j-web::jar:2.19.0[paths: H:\Maven\org\apache\logging\log4j\log4j-web\2.19.0\log4j-web-2.19.0.jar;]] contain duplicate files, e.g. META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat