3 Replies Latest reply on Nov 28, 2011 1:57 PM by jamezp

    Using log4j on JBossAS7

    tc7

      I'll preface the following by saying I've read all the logging configuration type links from the JBoss as7 documentation, but still don't fully understand how logging is configured for my application. Suffice to say we had log4j working happily on JBoss 4.2.3.GA, and with one or two changes to the way log4j is initialised / configured (we removed all log4j.properties and log4j.xml files) it's been working reasonably with JBoss-as-7.0.2.Final with the following exception:

       

      1) I don't seem to be able to prevent a more specialised logger from (also) logging to the parent logger - despite setting: use-parent-handlers="false" in standalone-preview.xml.

                  <logger category="com.myapp.wal" use-parent-handlers="false">

                      <level name="DEBUG"/>

                      <handlers>

                        <handler name="wal"/>

                      </handlers>

                  </logger>

      In order to prevent DEBUG logs from ALSO going to the main CONSOLE/FILE logs I have to set the appender level to INFO.

       

      Has anyone else had success with the "use-parent-handlers" attribute?

       

       

      2) Querrying the log level specifially doesn't seem to work on a specific logger (whose level has been specifically set in standlone-preview.xml).

       

                  <logger category="myapp.MyClass-mylabel">

                      <level name="DEBUG"/>

                  </logger>

       

      a) org.apache.log4j.Logger.getLogger("myapp.MyClass-mylabel").getLevel() --> returns null

      b) org.apache.log4j.Logger.getLogger("myapp.MyClass-mylabel").getEffectiveLevelLevel() --> returns DEBUG

      c) org.apache.log4j.Logger.getLogger("myapp.MyClass-mylabel").isDebugEnabled() --> returns DEBUG

      d) java.util.logging.Logger.getLogger("myapp.MyClass").getLevel() --> returns DEBUG

       

      I'd expect null in a) above if the parent logger (only) was DEBUG.

      The logger and correct log level is displayed in the logging component of the web console.

       

      We make limited use of getLevel(), however in certain cases it's useful to get the actual log level of a particular category - not the inherited level.

      We have a large number of classes with references to log4j - so we're not ready to start using JDK logger just yet.

       

      Has anyone else encountered this problem?

      Any suggestions?

        • 1. Re: Using log4j on JBossAS7
          jamezp

          Do you have the handler named "wal" registered as a handler under the root-logger? If so that would be why you see the message.

           

          I'd have to see the logging portion of your standalone.xml to see what might be happening there. We do use JBoss Logging 3 in JBoss AS7 and not log4j.

          • 2. Re: Using log4j on JBossAS7
            tc7

            The following expands on the partial configuration illustrated above for question 1a).

             

            My root logger section is defined as:

                        <root-logger>

                            <level name="INFO"/>

                            <handlers>

                                <handler name="CONSOLE"/>

                                <handler name="FILE"/>

                            </handlers>

                        </root-logger>

             

            And the CONSOLE/FILE/wal loggers:

                    <subsystem xmlns="urn:jboss:domain:logging:1.1">       

                        <console-handler name="CONSOLE">

                            <level name="INFO"/> <!-- set to DEBUG to avoid duplicate logs as mentioned above -->

                            <formatter>

                                <pattern-formatter pattern="%d{yyyyMMdd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                            </formatter>

                        </console-handler>

                        <!-- my logging customisation -->

                        <periodic-rotating-file-handler name="FILE">

                            <level name="DEBUG"/>

                            <formatter>

                                <pattern-formatter pattern="%d{yyyyMMdd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                            </formatter>

                            <file relative-to="jboss.server.log.dir" path="myapp.log"/>

                            <suffix value=".yyyy-MM-dd"/>

                        </periodic-rotating-file-handler>

                        ...

                        <periodic-rotating-file-handler name="wal">

                            <level name="DEBUG"/>

                            <formatter>

                                <pattern-formatter pattern="%d{yyyyMMdd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                            </formatter>

                            <file relative-to="jboss.server.log.dir" path="wal.log"/>

                            <suffix value=".yyyy-MM-dd"/>

                        </periodic-rotating-file-handler>

                   </subsystem

             

            Must wal be defined as a root log handler in order to prevent wal DEBUG level messages also displaying in the FILE/CONSOLE logs above? I thought that was the purpose of the use-parent-handlers attribute.

            • 3. Re: Using log4j on JBossAS7
              jamezp

              Sorry it took so long to respond. I don't see anything obviously wrong with your configuration. I did test on JBoss AS7 Beta1 and it seemed to work for me.

               

              Here is the configuration I used:

               

              <subsystem xmlns="urn:jboss:domain:logging:1.1">
                          <console-handler name="CONSOLE">
                              <level name="INFO"/>
                              <formatter>
                                  <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
                              </formatter>
                          </console-handler>
                          <!-- Testing a debug file -->
                          <file-handler name="fh" autoflush="true">
                              <file relative-to="jboss.server.log.dir" path="fh.log"/>
                          </file-handler>
                          <periodic-rotating-file-handler name="FILE">
                              <level name="DEBUG"/>
                              <formatter>
                                  <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
                              </formatter>
                              <file relative-to="jboss.server.log.dir" path="server.log"/>
                              <suffix value=".yyyy-MM-dd"/>
                              <append value="false"/>
                          </periodic-rotating-file-handler>
                          <logger category="com.arjuna">
                              <level name="WARN"/>
                          </logger>
                          <logger category="org.apache.tomcat.util.modeler">
                              <level name="WARN"/>
                          </logger>
                          <logger category="sun.rmi">
                              <level name="WARN"/>
                          </logger>
                          <logger category="jacorb">
                              <level name="WARN"/>
                          </logger>
                          <logger category="jacorb.config">
                              <level name="ERROR"/>
                          </logger>
                         <!-- Only send messages to the fh handler -->
                          <logger category="org.jboss.as" use-parent-handlers="false">
                              <level name="DEBUG"/>
                              <handlers>
                                  <handler name="fh"/>
                              </handlers>
                          </logger>
                          <root-logger>
                              <level name="INFO"/>
                              <handlers>
                                  <handler name="CONSOLE"/>
                                  <handler name="FILE"/>
                              </handlers>
                          </root-logger>
                      </subsystem>
              

               

              This only wrote org.jboss.as messages to the fh.log file.