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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Bob Pepersack 410-468-2054


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to