You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by an...@itginc.com on 2004/01/30 16:13:09 UTC

RE: extending log4j Logger class? How to?

How about this.

As Donald suggested, create a custom message class.  Then create a custom
appender (or extend an existing one, like JDBCAppender).  Then you can
implement the append method any way you want.  You can retrieve your custom
message object from the LogEvent passed in to the appender's append()
method.

public class MyLogMessage {
  private String string;
  private byte[] bytes;
  private Document document;

  public String toString() {
    // return the string you want to show up in regular logs
}

public class MyAppender extends AppenderSkeleton {
  public void append(LoggingEvent event) {
    Object obj = event.getMessage();
    if (obj instanceof MyLogMessage) {
      MyLogMessage message = (MyLogMessage)obj;
      // add code to log message to database
    }
}

This way, you don't have to extend the Logger class, and your log
statements will look like Donald's example.  Also, if you're using another
appender, like file or console, they will contain the string value returned
by MyLogMessage.toString().

Hope that helps.

Regards,

Andy Nguyen



|---------+---------------------------->
|         |           "Ian Huynh"      |
|         |           <ianh@hubspan.com|
|         |           >                |
|         |                            |
|         |           01/29/2004 11:28 |
|         |           PM               |
|         |           Please respond to|
|         |           "Log4J Users     |
|         |           List"            |
|         |                            |
|---------+---------------------------->
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |                                                                                                                                                  |
  |       To:       "Log4J Users List" <lo...@logging.apache.org>                                                                               |
  |       cc:                                                                                                                                        |
  |       Subject:  RE: extending log4j Logger class? How to?                                                                                        |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|




Donald,
Hmm the ObjectRenderer interface returns a string.

What I'd really like is to allow a CustomAppender to receive the 3 params
either
as 3 params or via another MyLogObject().. but let the CustomAppender do
what it needs to do with it.

For example, I'd like the log the String, byte[], Document into a Oracle
Table
of 3 columns  VARCHAR(256), BLOB, CLOB.

Ian.


> -----Original Message-----
> From: Donald Larmee | ALTERthought [mailto:dlarmee@alterthought.com]
> 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: log4j-user-unsubscribe@logging.apache.org
> >For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>
> _______________________________________
>
>    Donald H. Larmee
>    ALTERthought, Inc.
>    804.301.8867 (c)
>
>    www.alterthought.com
> _______________________________________
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

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











-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
This message is for the named person's use only. This communication is for 
informational purposes only and has been obtained from sources believed to 
be reliable, but it is not necessarily complete and its accuracy cannot be 
guaranteed. It is not intended as an offer or solicitation for the purchase
or sale of any financial instrument or as an official confirmation of any
transaction. Moreover, this material should not be construed to contain any
recommendation regarding, or opinion concerning, any security. It may
contain confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or lost by any mistransmission. If
you receive this message in error, please immediately delete it and all
copies of it from your system, destroy any hard copies of it and notify the
sender. You must not, directly or indirectly, use, disclose, distribute, 
print, or copy any part of this message if you are not the intended 
recipient.  Any views expressed in this message are those of the individual
sender, except where the message states otherwise and the sender is 
authorized to state them to be the views of any such entity.

ITG Inc. reserves the right to monitor and archive all electronic 
communications through its network. 

ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-


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