3 Replies Latest reply on Apr 14, 2014 8:26 PM by ramyamagham

    Custom-handler is not loading the appenders and logging is not functioning properly

    ramyamagham

      I created a custom-handler, looked at many examples about configuring it on the internet and though all the configuration I have seems right, I do not see anything getting logged in the log file. I am using Jboss EAP 6.1.0.GA (AS 7.2.0.Final-redhat-8) and jboss-logmanager-1.4.3.Final.jar and jboss-logging-3.1.2.GA-redhat-1.jar files. The standalone-full.xml looks like below with the custom handler configuration,

       

      ...

                  <custom-handler name="TestTrailAppender" class="org.jboss.logmanager.handlers.MySizeRotatingFileHandler" module="org.jboss.logmanager">

                      <level name="INFO"/>

                      <formatter>

                          <pattern-formatter pattern="%d %-5p [%C{1}] %m%n"/>

                      </formatter>

                      <properties>

                          <property name="file" value="${jboss.server.base.dir}/log.0/logs/testtrail.txt"/>

                          <property name="append" value="true"/>

                          <property name="rotateSize" value="100"/>

                          <property name="maxBackupIndex" value="10"/>

                      </properties>

                  </custom-handler>

      ...

                  <logger category="my.testfreshtrail" use-parent-handlers="false">

                      <level name="DEBUG"/>

                      <handlers>

                          <handler name="TestTrailAppender"/>

                      </handlers>

                  </logger>

      ......

       

      MySizeRotatingFileHandlerlooks like below, which is added as a separate jar with only this java class and inside the "system/layers/base/org/jboss/logmanager/main" directory as a resource.

       

      package org.jboss.logmanager.handlers;

       

      public class MySizeRotatingFileHandler extends SizeRotatingFileHandler{

       

       

        private long rotateSize = 0xa0000L;

          private int maxBackupIndex = 1;

          private CountingOutputStream outputStream;

       

      public MySizeRotatingFileHandler() {

          super();

          System.out.println("in sysout 1..");

          }

       

      public void setOutputStream(final OutputStream outputStream) {

              synchronized (outputLock) {

                  this.outputStream = outputStream == null ? null : new CountingOutputStream(outputStream);

                  super.setOutputStream(this.outputStream);

              }

              System.out.println("in sysout 9..");

          }

       

      public void setFile(final String file) throws FileNotFoundException {

              synchronized (outputLock) {

                  super.setFile(new File(file));

                  if (outputStream != null)

                      outputStream.currentSize = file == null ? 0L : file.length();

              }

              System.out.println("in sysout 10..");

          }

       

      public void setRotateSize(final long rotateSize) {

              checkAccess(this);

              synchronized (outputLock) {

                  this.rotateSize = rotateSize;

              }

              System.out.println("in sysout 11..");

          }

       

      public void setMaxBackupIndex(final int maxBackupIndex) {

              checkAccess(this);

              synchronized (outputLock) {

                  this.maxBackupIndex = maxBackupIndex;

              }

              System.out.println("in sysout 12..");

          }

       

      @Override

        protected void preWrite(final ExtLogRecord record) {

        System.out.println("in sysout 13..");

              final int maxBackupIndex = this.maxBackupIndex;

              final long currentSize = (outputStream == null ? Long.MIN_VALUE : outputStream.currentSize);

              if (currentSize > rotateSize && maxBackupIndex > 0) {

                  try {

                      final File file = getFile();

                      if (file == null) {

                          // no file is set; a direct output stream or writer was specified

                          return;

                      }

                   

                      // rotate.  First, drop the max file (if any), then move each file to the next higher slot.

                      new File(file.getAbsolutePath() + "." + maxBackupIndex).delete();

       

      //I have a requirement to add timestamp to the file instead, so removed the existing for loop.

                      file.renameTo(new File(file.getAbsolutePath() + "." + System.currentTimeMillis() + ".log"));

                      // start with new file.

                      setFile(file);

                  } catch (FileNotFoundException e) {

                      reportError("Unable to rotate log file", e, ErrorManager.OPEN_FAILURE);

                  }

              }

          }

      }

       

      And I use it with the following code.

       

      myRefreshLogger = org.apache.log4j.Logger.getLogger(softwareComponent, LoggerFactory.getInstance("my.testfreshtrail"));

      myRefreshLogger.info("This is a test message");

       

       

      I tried looking into Jboss code and noticed the handlers are getting loaded without any errors from the standalone-full.xml inside class org.jboss.logmanager.LoggerNode in setHandler and addHandler methods and I could read them from "handlers" member variable. But inside the publish() method of this class, I get directed to doPublish() method of JBossAppenderHandler class and then the getAllAppenders(logger) method inside it returns zero size list back. Where am I doing wrong? Isn't the flow supposed to go to doPublish method of WriterHandler hierarchy? Also from the above code the STDOUT logs only "in sysout 1..", 9, 10, 11 and 12. The 13th one which is overridden method is never called. There are absolutely no errors reported in the application during start up or during the call to log info messages to the log file. Where do I correct this, any suggestions please?