logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rodriguez, Victor" <vrodrig...@Intrado.com>
Subject RE: URGENT: Overriding the forcedLog method
Date Fri, 16 Apr 2004 20:47:12 GMT
I don't think this would work in my case.  Let me elaborate...

I've customized the NTEventLogAppender.dll by adding a new reportEvent
method which takes a category and an event id so as to expand on the
limited support for windows event log categories and event id's that
comes built in to the NTEventLogAppender.dll and
org.apache.log4j.nt.NTEventLogAppender class.

To take advantage of this new reportEvent method, I've had to create my
own ElmNTEventLogAppender class to call it.  The append method takes a
LoggingEvent, so I have to create derive my own ElmLoggingEvent class to
contain the new nt_category and nt_event_id attributes.  The
LoggingEvent gets created in the forcedLog method of the Logger class.
So that's why I have to derive my own Logger to override the forcedLog
method to create an ElmLoggingEvent object instead of the standard
LoggingEvent object.

-----Original Message-----
From: Robert Pepersack [mailto:rpepersack@mdinsurance.state.md.us] 
Sent: Friday, April 16, 2004 14:09
To: Log4J Developers List
Subject: Re: URGENT: Overriding the forcedLog method

Don't extend Logger.  Wrap it using the GoF Decorator pattern (a.k.a
Wrapper).  There is a good example of how to wrap Logger in The Complete
log4j Manual.  Ceki also discusses the reasons why you should not extend
Logger.  Here is the source code for Ceki's Logger wrapper:

package chapter8;

import chapter8.XLevel;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import java.lang.reflect.Method;

public class MyLogger {
   // Our fully qualified class name.
   static String FQCN = MyLogger.class.getName();
   static boolean JDK14 = false;

   static {
     String version =  System.getProperty("java.version");
     if(version != null) {
       JDK14 = version.startsWith("1.4");
     }
   }

   private Logger logger;

   public MyLogger(String name) {
     this.logger = Logger.getLogger(name);
   }

   public MyLogger(Class clazz) {
     this(clazz.getName());
   }

   public void trace(Object msg) {
     logger.log(FQCN, XLevel.TRACE, msg, null);
   }
   public void trace(Object msg, Throwable t) {
     logger.log(FQCN, XLevel.TRACE, msg, t);
     logNestedException(XLevel.TRACE, msg, t);
   }
   public boolean isTraceEnabled() {
     return logger.isEnabledFor(XLevel.TRACE);
   }

   public void debug(Object msg) {
     logger.log(FQCN, Level.DEBUG, msg, null);
   }
   public void debug(Object msg, Throwable t) {
     logger.log(FQCN, Level.DEBUG, msg, t);
     logNestedException(Level.DEBUG, msg, t);
   }
   public boolean isDebugEnabled() {
     return logger.isDebugEnabled();
   }

   public void info(Object msg) {
     logger.log(FQCN, Level.INFO, msg, null);
   }
   public void info(Object msg, Throwable t) {
     logger.log(FQCN, Level.INFO, msg, t);
     logNestedException(Level.INFO, msg, t);
   }
   public boolean isInfoEnabled() {
     return logger.isInfoEnabled();
   }

   public void warn(Object msg) {
     logger.log(FQCN, Level.WARN, msg, null);
   }
   public void warn(Object msg, Throwable t) {
     logger.log(FQCN, Level.WARN, msg, t);
     logNestedException(Level.WARN, msg, t);
   }

   public void error(Object msg) {
     logger.log(FQCN, Level.ERROR, msg, null);
   }
   public void error(Object msg, Throwable t) {
     logger.log(FQCN, Level.ERROR, msg, t);
     logNestedException(Level.ERROR, msg, t);
   }

   public void fatal(Object msg) {
     logger.log(FQCN, Level.FATAL, msg, null);
   }
   public void fatal(Object msg, Throwable t) {
     logger.log(FQCN, Level.FATAL, msg, t);
     logNestedException(Level.FATAL, msg, t);
   }

   void logNestedException(Level level, Object msg, Throwable t) {
     if(t == null)
       return;

     try {
       Class tC = t.getClass();
       Method mA[] = tC.getMethods();
       Method nextThrowableMethod = null;
       for(int i=0; i < mA.length ; i++) {
         if(("getCause".equals(mA[i].getName()) && !JDK14)
            || "getRootCause".equals(mA[i].getName())
            || "getNextException".equals( mA[i].getName())
            || "getException".equals( mA[i].getName())) {
           // check param types
           Class params[] = mA[i].getParameterTypes();
           if(params==null || params.length==0) {
             // just found the getter for the nested throwable
             nextThrowableMethod=mA[i];
             break; // no need to search further
           }
         }
       }

       if(nextThrowableMethod != null) {
         // get the nested throwable and log it
         Throwable nextT=(Throwable)nextThrowableMethod.invoke(t, new
Object[0]);
         if(nextT != null) {
           this.logger.log(FQCN, level, "Previous log CONTINUED: ",
nextT);
         }
       }
     } catch(Exception e) {
       // do nothing
     }
   }
}

I hope this helps.  Credit to Ceki.

Bob Pepersack


At 01:33 PM 04/16/2004 -0600, you wrote:
>Hi!
>
>I need to derive my own Logger that overrides the forcedLog method so 
>as to use my own ElmLoggingEvent derived from the LoggingEvent class.
>Something like this.
>
>public class ElmLogger extends Logger {
>     protected ElmLogger(String s) {
>         super(s);
>     }
>
>     protected void forcedLog(String fqcn, Priority level, Object 
>message, Throwable t) {
>           /*
>                 Insert custom code here...
>           */
>         callAppenders(new ElmLoggingEvent(/* customer args here*/));
>     }
>}
>
>Now, HOW do I ensure that my ElmLogger class is used instead of the 
>Logger class?
>
>Thanks in advance for a PROMPT reply!
>
>Cheers...
>
>Victor M. Rodriguez
>Software Engineer
>
>Intrado, Inc.
>1601 Dry Creek Drive
>Longmont, CO  80501
>
>Tel: +1-720-864-5098
>Fax: +1-720-494-6600
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-dev-help@logging.apache.org

Bob Pepersack
410-468-2054


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message