6 Replies Latest reply on Jun 10, 2016 1:22 PM by Bernard H

    SLF4J Logging in AS 7.1.2.Final

    jmighion Newbie

      We want to use slf4j to log in our application, but it does not seem to be working.  When our application was running on AS 7.0.1 we had slf4j logging working.  After upgrading to EAP 6 the slf4j logging stopped.  What configurations are needed to use that logging facade?

       

      In the pom.xml :

       

      <properties>

                          <version.arquillian_core>1.0.0.CR5</version.arquillian_core>

                          <jboss.version>7.1.0.Beta1</jboss.version>

                          <junit.version>4.8.1</junit.version>

                          <slf4j.version>1.6.1</slf4j.version>

      </properties>

      ...

       

      <dependency>

                                    <groupId>log4j</groupId>

                                    <artifactId>log4j</artifactId>

                                    <version>1.2.16</version>

      </dependency>

      <dependency>

                                    <groupId>org.slf4j</groupId>

                                    <artifactId>slf4j-api</artifactId>

                                    <version>1.6.1</version>

      </dependency>

      <dependency>

                                    <groupId>org.slf4j</groupId>

                                    <artifactId>slf4j-log4j12</artifactId>

                                    <version>1.6.1</version>

                          </dependency>

      <dependency>

                                    <groupId>org.slf4j</groupId>

                                    <artifactId>jcl-over-slf4j</artifactId>

                                    <version>1.6.1</version>

      </dependency>

      <dependency>

                                    <groupId>commons-logging</groupId>

                                    <artifactId>commons-logging</artifactId>

                                    <version>1.1.1</version>

      </dependency>

       

      the log4j.xml :

       

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

      <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

      <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

                <appender name="CA" class="org.apache.log4j.ConsoleAppender">

                          <layout class="org.apache.log4j.PatternLayout">

                                    <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />

                          </layout>

                </appender>

                <appender name="FA" class="org.apache.log4j.FileAppender">

                          <param name="File" value="epc-frontend.log" />

                          <param name="Threshold" value="WARN" />

                          <layout class="org.apache.log4j.PatternLayout">

                                    <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />

                          </layout>

                </appender>

                <root>

                          <level value="DEBUG" />

                          <appender-ref ref="CA" />

                          <appender-ref ref="FA" />

                </root>

      </log4j:configuration>

       

      I had tried using jboss-deployment-structure.xml with :

       

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

      <jboss-deployment-structure>

        <deployment>

            <dependencies>

               <module name="org.slf4j" />

            </dependencies>

         </deployment>

         <exclusions>

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

         </exclusions>

      </jboss-deployment-structure>

       

      I tried both with and without the exclusion.  Though, with the exclusion I get log4j:ERROR Could not create an Appender. Reported error follows. for both :

      java.lang.ClassCastException: org.apache.log4j.ConsoleAppender cannot be cast to org.apache.log4j.Appender

      java.lang.ClassCastException: org.apache.log4j.FileAppender cannot be cast to org.apache.log4j.Appender

       

      The manifest.mf has org.slf4j as a dependency too.

      Are there exlipcit changes to standalone.xml that are now necessary?

        • 1. Re: SLF4J Logging in AS 7.1.2.Final
          Stephen Coy Master

          I can see a few issues here.

          1. You will get inconsistent behaviour by having both commons-logging and jcl-over-slf4j in your application. They both contain different implementations of the same classes. See http://www.slf4j.org/legacy.html.
          2. You should include your own copy of slf4j jars in this scenario, as the module will not be able to "see" the log4j implementation in your application
          3. If your deployment is a just a war then you should not need the jboss-deployment-structure.xml as JBoss should use your WAR based classes first
          4. If your deployment is an EAR, then every module must contain it's own jboss-deployment-structure.xml file (which would now exclude the slf4j module as well).

           

          You will find life much simpler if you just use the slf4j APIs and ride on top of the server logging implementation. There are many benefits to this:

          1. Cluster wide logging configuration
          2. Easy on the fly reconfiguration from the admin console
          3. Simplifies your build and deployment
          • 2. Re: SLF4J Logging in AS 7.1.2.Final
            Riccardo Pasquini Novice

            Easy life, i agree with Stephen, moreover you can set org.slf4j as a provided dependency, just remember to set the org.slf4j jboss-module dependency in your manifest files

             

            you can set the slf4j-log4j12 and log4j as dependencies with test scope and place a log4j.xml config file in your (filtered) test resources, use filtering to place the log files in the target directory

             

            bye

            • 3. Re: SLF4J Logging in AS 7.1.2.Final
              James Perkins Master

              You don't need to add the dependency to your manifest. By default jboss-logging, log4j, slf4j and commons-logging are added to deployments.

              • 4. Re: SLF4J Logging in AS 7.1.2.Final
                johgusta Newbie

                I experienced the same problem when migrating to EAP 6.0 where slf4j logging stopped working. What finally did work for me was to remove the log4j.xml entirely and get the same behaviour as in 7.1.1 after finding this thread: https://community.jboss.org/thread/199923

                • 5. Re: SLF4J Logging in AS 7.1.2.Final
                  jmighion Newbie

                  Thanks, that seemed to do the trick!

                  • 6. Re: SLF4J Logging in AS 7.1.2.Final
                    Bernard H Newbie

                    I wanted to use SLF4J (instead of jboss native logging as documented in dev manuals) in JBoss EAP 7 executed in Domain mode. All Traces were missing both from deployed WARs, and EARs until I discovered that if you do remove everything but your SLF4J logger calls in java source code of course, it actually works!

                    • using Maven or any other build tools, ensure you have ONLY the SLF4J API dependency and that it is set in provided mode so has to build your code but not include the dependency in the build output
                    • do ensure too that you do not have any log4j.xml or other logging configuration file in the deployed war or ear
                    • you may post check that the built jars, wars, and ears actually do not contain any slf4j-api jar, no slf4j binding/mapping to other logging frameworks (e.g. slf4j-log4j, slf4j-simple, logback, etc.), no log config resource either, actually nothing related to the logging API or logging handlers
                    • you control the logging levels and log handlers via the web administration console of your JBoss domain (else edit the domain.xml while the server is not running)

                    and everything works fine...