The log4j Complete Manual specifically recommends against subclassing the
Logger class (pg. 161-2). Here is the code I have been using. It mimics the
standard log4j interface by using the same names, etc., and allows me to add
extensions. The only thing that needs to be changed to switch from standard
log4j to this wrapper version is the import statement. 

Jim


import org.apache.log4j.Level;

public class Logger {

    // ---------- Instance variables:

    /**
     * The log4j <code>Logger</code> instance associated with this logger.
     */
    org.apache.log4j.Logger logger;  // Log4j Logger instance.

    // ---------- Constructors:

    /**
     * Create a <code>Logger</code> object named using the parameter
     * <code>loggerName</code>.
     *
     * @param loggerName    The name given to the Logger object.
     */
    private Logger( String loggerName ) {
        this.logger = org.apache.log4j.Logger.getLogger( loggerName );
    }

    /**
     * Get a <code>Logger</code> object named using the parameter
     * <code>loggerName</code>. The object returned will be appropriate to 
     * the caller's environment (Application Server tier, Web Server tier, 
     * etc.).
     *
     * @param loggerName    The name given to the Logger object.
     */
    public static Logger getLogger( String loggerName ) {
        return new Logger( loggerName );
    }

    /**
     * Get a <code>Logger</code> object named using the class name of the
     * parameter <code>clazz</code>.
     *
     * @param clazz         The name of this class is given to the Logger
     *                      object.
     */
    public static Logger getLogger( Class clazz ) {
        return getLogger( clazz.getName() );
    }

    // ---------- Logging methods:

    /**
     * Write a message with the <code>DEBUG</code> severity to the log.
     *
     * @param message       The message to log.
     */
    public final void debug( String message ) {
        this.logger.log( Logger.class.getName(), Level.DEBUG, message, null
);
    }

    /**
     * Write a message and exception with the <code>DEBUG</code> severity to
     * the log.
     *
     * @param message       The message to log.
     * @param error                 The exception or error to log.
     */
    public final void debug( String message, Throwable error ) {
        this.logger.log( Logger.class.getName(), Level.DEBUG, message, error
);
    }

    /*
     * etc.
     */

}

-----Original Message-----
From: Ian Huynh [mailto:[EMAIL PROTECTED] 
Sent: Thursday, January 29, 2004 11:58 PM
To: Log4J Users List
Subject: RE: extending log4j Logger class? How to? 

Actually I just thought of something 

1. Implement a custom logfactory 

class MyLog4JFactory implements LoggerFactory {
     public MyLog4JFactory () {
     }

     public Logger makeNewLoggerInstance(String name) {
        return new MyOwnLogger(name);
     }
}

2. and use Logger.getLogger("someclass", new MyLog4JFactory() )... 

still stuck here, Is there a way to override the default factory? otherwise,
I am still back at modifying a lot of code.





> -----Original Message-----
> From: Donald Larmee | ALTERthought [mailto:[EMAIL PROTECTED]
> Sent: Thursday, January 29, 2004 5:46 PM
> To: Log4J Users List
> Subject: Re: extending log4j Logger class? How to? 
> 
> 
> if you can pack all of the data you would like to log into 
> one wrapper 
> object, I would look into using an ObjectRenderer to do what 
> you suggest. i.e.,
> 
>     logger.debug(new MyLogObject(String,byte[], Document), Throwable);
> 
> You would then provide an ObjectRenderer that undertands how 
> to handle a 
> MyLogObject...etc...
> 
> -d
> 
> 
> At 08:28 PM 1/29/2004, you wrote:
> 
> >I would like to debug more than just a string so would it 
> make sense to 
> >extend the Log4J Logger?
> >
> >eg.  let say, i'd like to have the following interface
> >
> >          Logger.debug( String, byte[], Document, Throwable)
> >
> >I can certainly use a wrapper pattern (see below) but would 
> have to make 
> >massive changes to my code.
> >
> >public class MyLogger {
> >    public MyLogger (Logger log4jLogger) {}
> >}
> >
> >So the alternative is,  I tried to extend the Logger Class 
> but is unsure 
> >how to get the LoggerFactory to return MyLogger instead of 
> Logger class.
> >
> >Is there an easier way to accomplish this ?
> >
> >Thanks.
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> _______________________________________
> 
>    Donald H. Larmee
>    ALTERthought, Inc.
>    804.301.8867 (c)
> 
>    www.alterthought.com
> _______________________________________ 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

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

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

Reply via email to