1 Reply Latest reply on May 6, 2011 9:33 AM by jaikiran pai

    Adding a custom appender to log4j

    Avi Chapman Newbie

      Hi all,

       

      I'm having a hard time adding a custom appender to log4j in JBoss 6.0. I've created my own custom appender class:

       

      package com.baesystems.icg.gateway.auditengine;
      
      
      import org.apache.log4j.AppenderSkeleton;
      import org.apache.log4j.spi.LoggingEvent;
      import org.apache.log4j.xml.XMLLayout;
      
      
      /***
       * This appender takes messages from log4j and adds them to the
       * Audit engine.
       * @author anchapma
       *
       */
      public class AuditEngineAppender extends AppenderSkeleton
      {
          /***
           * Constructor.
           */
          public AuditEngineAppender()
          {
              // Temporary break point...
              System.out.println("APPENDER CONSTRUCTOR");
          }
      
          /***
           * Called once all the options have been set. This will set up the link to the 
           * audit engine.
           */
          @Override
          public void activateOptions()
          {
              System.out.println("APPENDER STARTING UP");
          }
      
          /***
           * Actually do the logging. The AppenderSkeleton's 
           * doAppend() method calls append() to do the
           * actual logging after it takes care of required
           * housekeeping operations.
           * @param event The event to log.
           */
          @Override
          protected void append(LoggingEvent event)
          {
              System.out.println("APPENDER.append()");
          }
      
      
          /***
           * Release any resources allocated within the appender such as file handles,
           * network connections, etc.
           */
          @Override
          public void close()
          {
          }
      
      
          /***
           * Configurators call this method to determine if the appender requires a layout.
           */
          @Override
          public boolean requiresLayout()
          {
              return true;
          }
      }
      
      
      
      

       

       

       

      This is configured to be used in my jboss-logging.xml file:

       

      <?xml version="1.0" encoding="UTF-8"?>
      
      
      <!-- ===================================================================== -->
      <!--                                                                       -->
      <!--  Logging System Configuration                                         -->
      <!--                                                                       -->
      <!-- ===================================================================== -->
      
      
      <logging xmlns="urn:jboss:logging:6.0" xmlns:b="urn:jboss:bean-deployer:2.0">
      
      
         <!-- ================================= -->
         <!-- Preserve messages in a local file -->
         <!-- ================================= -->
      
      
         <!-- A time/date based rolling handler -->
      
      
         <periodic-rotating-file-handler
               file-name="${jboss.server.log.dir}/server.log"
               name="FILE"
               autoflush="true"
               append="true"
               suffix=".yyyy-MM-dd">  <!-- To roll over at the top of each hour, use ".yyyy-MM-dd-HH" instead -->
      
      
            <error-manager>
               <only-once/>
            </error-manager>
      
      
            <formatter>
               <!-- To revert back to simple stack traces without JAR versions, change "%E" to "%e" below. -->
               <!-- Uncomment this to get the class name in the log as well as the category
               <pattern-formatter pattern="%d %-5p [%c] %C{1} (%t) %s%E%n"/>
               -->
               <!-- Uncomment this to log without the class name in the log -->
               <pattern-formatter pattern="%d %-5p [%c] (%t) %s%E%n"/>
            </formatter>
         </periodic-rotating-file-handler>
      
      
         <!-- ============================== -->
         <!-- Append messages to the console -->
         <!-- ============================== -->
      
      
         <console-handler name="CONSOLE" autoflush="true" target="System.out">
            <error-manager>
               <only-once/>
            </error-manager>
      
      
            <level name="INFO"/>
      
      
            <formatter>
               <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] %s%E%n"/>
            </formatter>
         </console-handler>
      
      
         <!-- ============================== -->
         <!-- Append messages to the console -->
         <!-- ============================== -->
      
      
         <log4j-appender name="AUDITMANAGER" class="com.baesystems.icg.gateway.auditengine.AuditEngineAppender">
             <formatter>
               <pattern-formatter pattern="%d %-5p [%c] %m%n"/>
            </formatter>
         </log4j-appender>
      
      
         <!-- =============================================== -->
         <!-- Limit categories                                -->
         <!-- if enabling "trace" at root logger level,       -->
         <!-- use limit category settings to disable trace    -->
         <!-- for packages that you don't want to trace       -->
         <!-- =============================================== -->
      
      
         <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
         <logger category="org.apache">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the jacorb category to WARN as its INFO is verbose -->
         <logger category="jacorb">
            <level name="WARN"/>
         </logger>
      
      
         <!-- Limit JSF to INFO as its FINE is verbose -->
         <logger category="javax.enterprise.resource.webcontainer.jsf">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the org.jgroups category to WARN as its INFO is verbose -->
         <logger category="org.jgroups">
            <level name="WARN"/>
         </logger>
      
      
         <!-- Limit the org.quartz category to INFO as its DEBUG is verbose -->
         <logger category="org.quartz">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the com.sun category to INFO as its FINE is verbose -->
         <logger category="com.sun">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the sun category to INFO as its FINE is verbose -->
         <logger category="sun">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the javax.xml.bind category to INFO as its FINE is verbose -->
         <logger category="javax.xml.bind">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the springframework category to WARN-->
         <logger category="org.springframework">
            <level name="WARN"/>
         </logger>
      
      
         <!-- Limit Arjuna transaction manager -->
         <logger category="com.arjuna.ats">
           <level name="INFO" />
         </logger>
      
      
         <!-- Limit the JSR77 categories -->
         <logger category="org.jboss.management">
            <level name="INFO"/>
         </logger>
      
      
         <!-- Limit the verbose facelets compiler -->
         <!-- Also suppress error with legacy facelets (JBAS-7600) -->
         <logger category="facelets.compiler">
            <level name="WARN"/>
            <filter>
               <not>
                  <match pattern="Error\sLoading\sLibrary.*jsf-libs/jsf-impl"/>
               </not>
            </filter>
         </logger>
      
         <!-- Limit the org.jboss.serial (jboss-serialization) to INFO as its DEBUG is verbose -->
         <logger category="org.jboss.serial">
            <level name="INFO"/>
         </logger>
      
      
          <!-- Reduce org.mc4j stuff in the logs -->
         <logger category="org.mc4j">
            <level name="WARN"/>
         </logger>
      
      
         <!-- This is a temporary workaround for https://jira.jboss.org/jira/browse/EMBJOPR-280 and
              should be removed once that bug if fixed in the bundled admin-console war. -->
         <logger category="org.rhq.plugins.jbossas5.ApplicationServerComponent">
            <level name="FATAL"/>
         </logger>
      
         <logger category="com.baesystems">
            <level name="DEBUG"/>
            <handlers>
               <handler-ref name="AUDITMANAGER"/>
            </handlers>
         </logger>
      
      
         <!-- ======================= -->
         <!-- Setup the Root category -->
         <!-- ======================= -->
      
      
         <root-logger>
            <!-- Set the root logger priority via a system property, with a default value. -->
            <level name="${jboss.server.log.threshold:INFO}"/>
            <handlers>
               <handler-ref name="AUDITMANAGER"/>
               <handler-ref name="CONSOLE"/>
               <handler-ref name="FILE"/>
            </handlers>
         </root-logger>
      
      
      </logging>
      
      

       

      When I go to run JBoss, I get the output "APPENDER CONSTRUCTOR", but I don't get any output to indicate that the append method is ever called. In the meanwhile, the CONSOLE appender is outputting messages as expected.

       

      Does anyone have any idea what I'm doing wrong?

       

      Thanks,

      Avi