0

I have a Keycloak EAR module, deployed on Keycloak 11.0. In my EAR module I want to use log4j2 logging library with slf4j. I successfully accomplished this by adding the following dependencies in the pom directly: log4j-slf4j-impl, log4j-api & log4j-core.

Wildfly logging dependencies are excluded by setting add-logging-api-dependencies to false. log4j2.xml is specified outside the packaged EAR and is referenced with log4j.configurationFile system property.

Now the problem. I also want Keycloak to use log4j2 but I cannot get this to work. Keycloak is using Jboss Logging wrapper which always picks up JbossLogManager no matter which logging provider I set - I always get either ClassNotFoundException or NoClassDefFoundError.

I realize there is a classpath problem but I am out of ideas at this point. I tried creating log4j-slf4j-impl, log4j-api & log4j-core Wildfly modules with the following command but I still get the same error.

./jboss-cli.sh --command="module add --name=org.apache.logging.log4j.log4j-api --resources=/Users/jernej/log4j-api-2.13.3.jar"

If I understand correctly, log4j2 should be in the classpath on Wildfly startup (when searching for LoggerProvider) and Jboss Logging findProvider method should return Log4j2LoggerProvider if modules are correctly added? How can I accomplished this?

Suppose I want to use log4j2 appender, located in separate library. If I add this library as a Wildfly module this log4j2 appender can then be used by other deployments as well - e.g. keycloak-server.war?

jernejl
  • 105
  • 3
  • 9
  • Keycloak is a server module and not part of your deployment. Therefore it logs through the system logging as configured in the logging subsystem. – James R. Perkins Nov 04 '20 at 18:54
  • I understand. Is there any way to pass those logs to log4j2 or any other custom appender? – jernejl Nov 05 '20 at 07:15
  • 1
    You can run them through any handler (appender) that extends a `java.util.logging.Handler`. https://docs.wildfly.org/20/wildscribe/subsystem/logging/custom-handler/index.html. You can't process them through log4j2-core however that itself is a log manager. – James R. Perkins Nov 05 '20 at 17:29
  • Thanks! Does this custom handler need to be installed as a module in order to be available to all deployments? – jernejl Nov 09 '20 at 12:46
  • 1
    Yes any custom handler would need to be installed as a module. – James R. Perkins Nov 09 '20 at 15:19
  • @jernejl can you be more specific about how you achieved to use log4j2 in your EAR module ? For instance, where did you put the log4j2.xml exactly and what its content is ? I'm trying to achive the same but didn't succeed to make it work so far. – Fabrice G. Apr 05 '21 at 15:27
  • @FabriceG., log4j2.xml is located outside of the EAR module and is referenced with the "log4j2.configurationFile" system property. I have Fluency logging appender defined inside, nothing unusual. – jernejl Apr 09 '21 at 08:26

1 Answers1

0

Adding a library as a module may not necessary makes it available to your application. Have you defined the dependency to that module in the jboss-deployment-structure.xml in the META-INF folder of your ear file? Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.3"
                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                            xsi:schemaLocation="urn:jboss:deployment-structure:1.3 http://www.jboss.org/schema/jbossas/jboss-deployment-structure-1_3.xsd">
    <deployment>
        <dependencies>
            <module name="org.apache.logging.log4j.log4j-api" export="true"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>
zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33
  • 1
    I tried similar thing described here: https://stackoverflow.com/questions/42243943/wildfly-10-logback. I got it working for my app but as James said in the comment above, Keycloak is not part of this deployment. – jernejl Nov 05 '20 at 07:29