7 Replies Latest reply on Feb 2, 2017 12:17 PM by jamezp

    How to add log4j2 to Wildfly modules?

    niksa.baldun

      I wanted to add log4j2 to Wildfly, so I added the following modules:

       

      $JBOSS_HOME/modules/system/layers/base/com/lmax/disruptor/main/module.xml

      <?xml version="1.0" encoding="UTF-8"?>

       

       

      <module xmlns="urn:jboss:module:1.1" name="com.lmax.disruptor">

          <resources>

              <resource-root path="disruptor-3.2.0.jar"/>

          </resources>

      </module>

      $JBOSS_HOME/modules/system/layers/base/org/apache/log4j2/core/main/module.xml

      <?xml version="1.0" encoding="UTF-8"?>

       

       


      <module xmlns="urn:jboss:module:1.1" name="org.apache.log4j2.core">

       

       

          <resources>

              <resource-root path="log4j-core-2.0-beta9.jar"/>

          </resources>

       

       

          <dependencies>

              <module name="com.lmax.disruptor"/>

          </dependencies>

       

       

      </module>

      $JBOSS_HOME/modules/system/layers/base/org/apache/log4j2/main/module.xml

      <?xml version="1.0" encoding="UTF-8"?>

       

       


      <module xmlns="urn:jboss:module:1.1" name="org.apache.log4j2">

          <resources>

              <resource-root path="log4j-api-2.0-beta9.jar"/>

          </resources>

       

       

          <dependencies>

              <module name="org.apache.log4j2.core"/>

          </dependencies>

      </module>

       

      In jboss-deployment-structure, I added those modules as dependencies, so they are visible to my application:

       

      jboss-deployment-structure.xml

      ...

      <dependencies> 

          <module name="com.lmax.disruptor"/>

          <module name="org.apache.log4j2.core"/>

          <module name="org.apache.log4j2"/>

      </dependencies> 

      ...

       

      There are no errors when I start the application, but when I first try to log something, log4j2 complains with the following message:

       

      ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger

       

      Apparently, this message means that log4j-api-2.0-beta9.jar cannot access log4j-core-2.0-beta9.jar. But in module definition, I specified that log4j-core-2.0-beta9.jar is a dependency, so it should be on a classpath when log4j-api-2.0-beta9.jar is loaded. Or is my understanding of JBoss modules incorrect?

        • 1. Re: How to add log4j2 to Wildfly modules?
          jamezp

          I don't know how log4j2 finds it's implementation, but you might need to add services="import" to the org.apache.log4j2 module. Also were it me I'd probably just put the core library in the same module directory as the API and just add the resource to the org.apache.log4j2 module.

           

          --

          James R. Perkins

          • 2. Re: How to add log4j2 to Wildfly modules?
            niksa.baldun

            Thanks for the reply.


            The log4j2 API locates logging implementations by searching for META-INF/log4j-provider.properties file in the classpath. The log4j-core jar has that file inside it.


            About your second suggestion, I already tried it and it didn't work (I get the same error message).


            About your first suggestion, where exactly do I put services="import"?

            • 3. Re: Re: How to add log4j2 to Wildfly modules?
              jamezp

              Ah, in that case you need to explicitly import the path. You should end up with something like:

              <?xml version="1.0" encoding="UTF-8"?>
              <module xmlns="urn:jboss:module:1.1" name="org.apache.log4j2">
                  <resources>
                      <resource-root path="log4j-api-2.0-beta9.jar"/>
                  </resources>
              
              
                  <dependencies>
                      <module name="org.apache.log4j2.core">
                          <imports>
                              <include path="META-INF"/>
                          </imports>
                      </module>
                  </dependencies>
              </module>
              

               

              --

              James R. Perkins

              • 4. Re: Re: Re: How to add log4j2 to Wildfly modules?
                niksa.baldun

                Thanks, man, that is spot on. I also had to add some dependencies to log4j.core. I am including module.xml for future readers.

                 

                <?xml version="1.0" encoding="UTF-8"?>
                <module xmlns="urn:jboss:module:1.1" name="org.apache.log4j2.core">
                    <resources>
                        <resource-root path="log4j-core-2.0-beta9.jar"/>
                    </resources>
                
                    <dependencies>
                        <module name="org.apache.log4j2"/>
                        <module name="com.lmax.disruptor"/>
                        <module name="javax.api"/>
                        <module name="javax.servlet.api"/>
                    </dependencies>
                </module>
                
                • 5. Re: Re: Re: Re: How to add log4j2 to Wildfly modules?
                  niksa.baldun

                  Another note for future readers. This setup worked for some time, and then I suddenly started to receive the same error message again. Obviously, I must have changed something somewhere that influenced log4j2 module, but I could not find out what it is. Finally, I resolved it by putting the same filter in jboss-deployment-structure.xml.

                   

                  <?xml version="1.0" encoding="UTF-8"?>
                  <jboss-deployment-structure>
                      <deployment>
                          <exclusions>
                          </exclusions>
                          <dependencies>  
                              <module name="org.apache.log4j2.core">
                                  <imports>
                                      <include path="META-INF"/>
                                  </imports>
                              </module>
                          </dependencies>  
                      </deployment>
                  </jboss-deployment-structure>
                  
                  • 6. Re: How to add log4j2 to Wildfly modules?
                    jaymz2014

                    Hello,

                     

                    I tried your configuration but i have this error :

                     

                    Failed to define class org.apache.logging.log4j.core.impl.Log4jContextFactory in Module "org.apache.log4j2.core:main"...

                     

                    java.lang.NoClassDefFoundError: Failed to link org/apache/logging/log4j/core/impl/Log4jContextFactory (Module "org.apache.log4j2.core:main"...

                     

                    I can't see what is wrong...

                     

                    Thank you for you help

                    • 7. Re: How to add log4j2 to Wildfly modules?
                      jamezp

                      Could you share your module.xml file(s)? That message definitely looks a bit weird.

                       

                      --

                      James R. Perkins