3 Replies Latest reply on Jun 10, 2019 11:16 AM by jamezp

    Log4j2 WildFly 9  web application - log file empty

    aliosha79

      I'm working on Wildfly 9/10. I wrote my web application with my custom Log4j2 configuration file.


      <configuration status="INFO">
        <Appenders>
        <RollingFile name="General" fileName="D:/logs/general/general.log"
        filePattern="D:/logs/general/$${date:yyyy-MM}/general-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout
        pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n" />
        <SizeBasedTriggeringPolicy size="20 MB" />
        </RollingFile>
        <Async name="asyncGeneral" bufferSize="10" includeLocation="true">
        <AppenderRef ref="General" />
        </Async>
      </Appenders>
      <Loggers>
        <Root level="INFO"> 
        <AppenderRef ref="asyncGeneral" />
        </Root>
      </Loggers>



      I chose to use slf4j as it offers me a good facade to interact with log4j2. These are my declared dependencies:

           <dependency>

                <groupId>org.slf4j</groupId>

                <artifactId>slf4j-api</artifactId>

                  <version>1.7.7</version>              

        </dependency>

        <dependency>

                <groupId>org.apache.logging.log4j</groupId>

                <artifactId>log4j-slf4j-impl</artifactId>

                <version>2.6</version>

        </dependency>

        <dependency>

                  <groupId>org.apache.logging.log4j</groupId>

                  <artifactId>log4j-api</artifactId>

                  <version>2.6</version>

        </dependency>

        <dependency>

                       <groupId>org.apache.logging.log4j</groupId>

                  <artifactId>log4j-core</artifactId>

                       <version>2.6</version>

        </dependency>

        <dependency>

                  <groupId>org.apache.logging.log4j</groupId>

                  <artifactId>log4j-web</artifactId>

                  <version>2.6</version>

        </dependency>



      The configuration file is correctly included inside a war jar lib.

      The result is that when i run the web application it generates the file:


          D:/logs/general.log


      But it continues to print the log on the server console, while the file keeps to remain empty.

      i have also tried to add the


       

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

      <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">

          <deployment>

              <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->

              <exclusions>

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

              </exclusions>

          </deployment>

      </jboss-deployment-structure>

       

      jboss-deplyment-structure.xml file inside the  WEB-INF folder of my web application


      i realized that when i deploy my application, the server print this debug level information:


      12:58:17,190 INFO [stdout] (ServerService Thread Pool -- 64) 2016-06-16 12:58:17,190 ServerService Thread Pool -- 64 DEBUG createAppender(={General}, errorRef="null", blocking="true", shutdownTimeout="0", bufferSize="10", name="asyncGeneral", includeLocation="true", Filter=null, Configuration(vfs:/C:/Windows/System32/content/sec-api.war/WEB-INF/lib/com.application.jar/log4j2.xml), ignoreExceptions="true")

      12:58:17,194 INFO [stdout] (ServerService Thread Pool -- 64) 2016-06-16 12:58:17,194 ServerService Thread Pool -- 64 DEBUG createAppenders(={Console, RoutingAppender, General,asyncGeneral})

      12:58:17,196 INFO [stdout] (ServerService Thread Pool -- 64) 2016-06-16 12:58:17,196 ServerService Thread Pool -- 64 DEBUG reateAppenderRef(ref="asyncGeneral", level="null", Filter=null)

      12:58:17,200 INFO [stdout] (ServerService Thread Pool -- 64) 2016-06-16 12:58:17,200 ServerService Thread Pool -- 64 DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={async, asyncGeneral, asyncDebug}, ={}, Configuration(vfs:/C:/Windows/System32/content/sec-api.war/WEB-INF/lib/com.klopotek.core-1.3.0-SNAPSHOT.jar/log4j2.xml), Filter=null)

      12:58:17,225 INFO [stdout] (ServerService Thread Pool -- 64) 2016-06-16 12:58:17,224 ServerService Thread Pool -- 64 DEBUG Registering MBean org.apache.logging.log4j2:type=/sec-api,component=AsyncAppenders,name=asyncGeneral



      The same configuration works properly on a standalone application.

      What am i missing?


      Is it possibile to integrate this machanism with Wildfly Logging Management through web administration console?

        • 1. Re: Log4j2 WildFly 9  web application - log file empty
          jamezp

          If you're using log4j as your logging facade you either need to add an exclusion for org.slf4j, exclude the logging subsystem or change the add-logging-api-dependencies attribute to false.

           

          Example jboss-deployment-structure.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
              <deployment>
                  <exclude-subsystems>
                      <subsystem name="logging" />
                  </exclude-subsystems>
              </deployment>
          </jboss-deployment-structure>
          

           

          CLI command to change the attribute (note this will cause all deployments to not get any default logging modules added)

          /subsystem=logging:write-attribute(name=add-logging-api-dependencies, value=false)
          

           

          --

          James R. Perkins

          • 2. Re: Log4j2 WildFly 9  web application - log file empty
            gaurav9822

            @James why do we need to exclude "logging" subsystem?

            • 3. Re: Log4j2 WildFly 9  web application - log file empty
              jamezp

              You only need to exclude the logging subsystem if you want to use your own version of log4j. By default WildFly will add a org.apache.log4j dependency to your deployment that works directly with the org.jboss.logmanager module. For WildFly 9 or higher as well you could just set the add-logging-api-dependencies to false as described above as well. Again though you only need to do this if you want to use your own version of log4j or use a custom slf4j binding.

               

              --

              James R. Perkins