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 David Hosier <da...@longviewsoftware.com> on 2006/05/08 02:49:29 UTC

Logger vs. Appender

Could anyone describe, or point me at resources that would describe, the
differences between creating your own Logger vs. creating your own Appender
for achieving custom logging behavior?  Thanks.

-David


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


RE: Logger vs. Appender

Posted by David Hosier <da...@longviewsoftware.com>.
Yes, it's working for me like a charm as well.  I just created a class
hierarchy for logging events.  Then I created an Appender and narrowed that
appender to only be used with very specific Categories.  Then in my code I
create a Logger for those specific Categories and pass instances of my
logging objects to the log calls.  On a database error, I even pass the
toString() of my logging objects up to the super Appender so that at least
something gets logged.  Thanks for the suggestions on this issue. 

-----Original Message-----
From: paul womack [mailto:pwomack@papermule.co.uk] 
Sent: Thursday, May 11, 2006 2:53 AM
To: Log4J Users List
Subject: Re: Logger vs. Appender

Curt Arnold wrote:
> 
> On May 8, 2006, at 1:33 AM, David Hosier wrote:
> 
> ...
> 
>>
>> Anyway...thanks again for the reply, looking forward to hearing other 
>> people's thoughts.
>>
> 
> I'd suggest creating a static helper class that generates a message 
> object and then calls logger methods.  The message object's toString 
> () should return a string that would be appropriate when the logging 
> request is handled by stock appenders like ConsoleAppender and 
> FileAppender, however your custom appender can get the message object 
> and get at the parts.

This approach certainly works; we've been using it for 2 years...

   BugBear

---------------------------------------------------------------------
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


Re: Logger vs. Appender

Posted by paul womack <pw...@papermule.co.uk>.
Curt Arnold wrote:
> 
> On May 8, 2006, at 1:33 AM, David Hosier wrote:
> 
> ...
> 
>>
>> Anyway...thanks again for the reply, looking forward to hearing other
>> people's thoughts.
>>
> 
> I'd suggest creating a static helper class that generates a message  
> object and then calls logger methods.  The message object's toString () 
> should return a string that would be appropriate when the logging  
> request is handled by stock appenders like ConsoleAppender and  
> FileAppender, however your custom appender can get the message object  
> and get at the parts.

This approach certainly works; we've been using it for 2 years...

   BugBear

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


Re: Logger vs. Appender

Posted by Curt Arnold <ca...@apache.org>.
On May 8, 2006, at 1:33 AM, David Hosier wrote:

...
>
> Anyway...thanks again for the reply, looking forward to hearing other
> people's thoughts.
>

I'd suggest creating a static helper class that generates a message  
object and then calls logger methods.  The message object's toString 
() should return a string that would be appropriate when the logging  
request is handled by stock appenders like ConsoleAppender and  
FileAppender, however your custom appender can get the message object  
and get at the parts.  The fragments could look something like:

public class FooBarLogger {
     private FooBarLogger() {}
     public static void info(final Logger logger, final Account  
account, final Transaction transaction) {
         if (logger.isInfoEnabled()) {
              logger.info(new FooBarMessage(account, transaction));
         }
     }
}

public class FooBarMessage {
     private final Account account;
     private final Transaction transaction;
     public FooBarMessage(final Account account, final Transaction  
transaction) {
         this.account = account;
         this.transaction = transaction;
   }
   public String toString() {
       return account.toString() + transaction.toString();
   }
   public Transaction getTransaction() {
       return transaction;
   }
   public Account getAccount() {
      return account;
   }
}

public class FooBarAppender extends AppenderSkeleton {
     ...
     public void doAppend(final LoggingEvent event) {
         if (event.getMessage() instanceof FooBarMessage) {
              ...
         } else {
             ...
         }
     }
}




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


RE: Logger vs. Appender

Posted by David Hosier <da...@longviewsoftware.com>.
Oops...I hit some strange keyboard combination and the email was sent
prematurely...I'll finish the email inline below.... 

-----Original Message-----
From: David Hosier [mailto:david@longviewsoftware.com] 
Sent: Monday, May 08, 2006 7:35 AM
To: 'Log4J Users List'
Subject: RE: Logger vs. Appender

So I was thinking about this in my sleep....I think I could probably just
create a custom Appender.  Then in my configuration file, I can apply that
Appender to a specific level for a specific Category like the following
Jboss example:

   <category name="org.jboss.system.server.Server">
     <priority value="INFO" />
     <appender-ref ref="TRAP_LOG"/>
   </category>

Then I can just use a common Category name for a Logger that needs to log my
events (since you don't HAVE to use the Class name as the Category name).
So if I want to log certain events with this Appender and others with the
normal Appenders (File and Console) in one Class, I can just create two
Logger instances in that Class, one with the Category set as the Class name
and the other with some common name.  I assume that I can use the log4j
configuration to limit the Appender's use to only specific Category and
Level configurations (i.e. I want the Appender to only be used by a specific
Category).  I'm assuming that the configuration example above only limits
the Category and Level combination to the Appender and not the other way
around.  Does this sound like the correct track to pursue?  Thanks.


-----Original Message-----
From: David Hosier [mailto:david@longviewsoftware.com]
Sent: Sunday, May 07, 2006 11:34 PM
To: 'Log4J Users List'
Subject: RE: Logger vs. Appender

Thanks for the response.  I need to create a logging solution that handles a
very specific set of actions, while simply using the standard Logger and
Appenders for everything else.  I've just always passed in String logging
messages and used the File and Console Appenders.  But now I need to put
only some logs into a database (which implies one of the JDBC Appenders that
are available).  However, the events that need to be logged to the database
will have different information depending on the event type (different
number of discrete data points, etc).  So that led me to think that
extending Logger would be better.  

My initial thought was to create a Class hierarchy that represented the
events I need to log to the database, and pass instances of those classes
into my own Logger.  The Logger would put different types into different
tables using the info() method, and then I would just pass all other levels
to the Logger superclass (just for completeness, since I would probably
never use the other levels).  So the basic issue is that I want to pass any
number of different Object types into the Logger calls, and I don't know how
best to handle that since I've only ever used Strings and Throwables in
logging calls before.  And I want to be clear that I don't want to just use
some JDBC Appender that takes strings and inserts them into a database with
some other information like a timestamp or other info that you might see in
the FileAppender.  For example, one event that needs to be logged may need
to insert three values into a database, while another might need to insert
five.

Anyway...thanks again for the reply, looking forward to hearing other
people's thoughts.

-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org]
Sent: Sunday, May 07, 2006 10:57 PM
To: Log4J Users List
Subject: Re: Logger vs. Appender

An appender corresponds to the destination of a logging request, like a
file, email, database record, network packet, etc.  Creating a custom
appender to support new types of destinations is a common practice and well
supported.

A logger corresponds to a topic of a logging request, like requests  
related to database activity, security or a particular class.   
Creating custom logger implementations is not a common practice.  The
typical motivation to create a custom logger implementation is to support
additional levels.  In most cases, the desire for additional new levels is
due to a misunderstanding of the role of logger names (to represent topics
or intended audience).  If you feel that a custom Appender is not adequate,
please describe to the list your needs and we should be able to help suggest
possibly solutions other than extending Logger.


On May 7, 2006, at 7:49 PM, David Hosier wrote:

> Could anyone describe, or point me at resources that would describe, 
> the differences between creating your own Logger vs. creating your own 
> Appender for achieving custom logging behavior?  Thanks.
>
> -David
>
>
> ---------------------------------------------------------------------
> 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



---------------------------------------------------------------------
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



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


RE: Logger vs. Appender

Posted by David Hosier <da...@longviewsoftware.com>.
So I was thinking about this in my sleep....I think I could probably just
create a custom Appender.  Then in my configuration file, I can apply that
Appender to a specific level for a specific Category like the following
Jboss example:


-----Original Message-----
From: David Hosier [mailto:david@longviewsoftware.com] 
Sent: Sunday, May 07, 2006 11:34 PM
To: 'Log4J Users List'
Subject: RE: Logger vs. Appender

Thanks for the response.  I need to create a logging solution that handles a
very specific set of actions, while simply using the standard Logger and
Appenders for everything else.  I've just always passed in String logging
messages and used the File and Console Appenders.  But now I need to put
only some logs into a database (which implies one of the JDBC Appenders that
are available).  However, the events that need to be logged to the database
will have different information depending on the event type (different
number of discrete data points, etc).  So that led me to think that
extending Logger would be better.  

My initial thought was to create a Class hierarchy that represented the
events I need to log to the database, and pass instances of those classes
into my own Logger.  The Logger would put different types into different
tables using the info() method, and then I would just pass all other levels
to the Logger superclass (just for completeness, since I would probably
never use the other levels).  So the basic issue is that I want to pass any
number of different Object types into the Logger calls, and I don't know how
best to handle that since I've only ever used Strings and Throwables in
logging calls before.  And I want to be clear that I don't want to just use
some JDBC Appender that takes strings and inserts them into a database with
some other information like a timestamp or other info that you might see in
the FileAppender.  For example, one event that needs to be logged may need
to insert three values into a database, while another might need to insert
five.

Anyway...thanks again for the reply, looking forward to hearing other
people's thoughts.

-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org]
Sent: Sunday, May 07, 2006 10:57 PM
To: Log4J Users List
Subject: Re: Logger vs. Appender

An appender corresponds to the destination of a logging request, like a
file, email, database record, network packet, etc.  Creating a custom
appender to support new types of destinations is a common practice and well
supported.

A logger corresponds to a topic of a logging request, like requests  
related to database activity, security or a particular class.   
Creating custom logger implementations is not a common practice.  The
typical motivation to create a custom logger implementation is to support
additional levels.  In most cases, the desire for additional new levels is
due to a misunderstanding of the role of logger names (to represent topics
or intended audience).  If you feel that a custom Appender is not adequate,
please describe to the list your needs and we should be able to help suggest
possibly solutions other than extending Logger.


On May 7, 2006, at 7:49 PM, David Hosier wrote:

> Could anyone describe, or point me at resources that would describe, 
> the differences between creating your own Logger vs. creating your own 
> Appender for achieving custom logging behavior?  Thanks.
>
> -David
>
>
> ---------------------------------------------------------------------
> 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



---------------------------------------------------------------------
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


RE: Logger vs. Appender

Posted by David Hosier <da...@longviewsoftware.com>.
Thanks for the response.  I need to create a logging solution that handles a
very specific set of actions, while simply using the standard Logger and
Appenders for everything else.  I've just always passed in String logging
messages and used the File and Console Appenders.  But now I need to put
only some logs into a database (which implies one of the JDBC Appenders that
are available).  However, the events that need to be logged to the database
will have different information depending on the event type (different
number of discrete data points, etc).  So that led me to think that
extending Logger would be better.  

My initial thought was to create a Class hierarchy that represented the
events I need to log to the database, and pass instances of those classes
into my own Logger.  The Logger would put different types into different
tables using the info() method, and then I would just pass all other levels
to the Logger superclass (just for completeness, since I would probably
never use the other levels).  So the basic issue is that I want to pass any
number of different Object types into the Logger calls, and I don't know how
best to handle that since I've only ever used Strings and Throwables in
logging calls before.  And I want to be clear that I don't want to just use
some JDBC Appender that takes strings and inserts them into a database with
some other information like a timestamp or other info that you might see in
the FileAppender.  For example, one event that needs to be logged may need
to insert three values into a database, while another might need to insert
five.

Anyway...thanks again for the reply, looking forward to hearing other
people's thoughts.

-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org] 
Sent: Sunday, May 07, 2006 10:57 PM
To: Log4J Users List
Subject: Re: Logger vs. Appender

An appender corresponds to the destination of a logging request, like a
file, email, database record, network packet, etc.  Creating a custom
appender to support new types of destinations is a common practice and well
supported.

A logger corresponds to a topic of a logging request, like requests  
related to database activity, security or a particular class.   
Creating custom logger implementations is not a common practice.  The
typical motivation to create a custom logger implementation is to support
additional levels.  In most cases, the desire for additional new levels is
due to a misunderstanding of the role of logger names (to represent topics
or intended audience).  If you feel that a custom Appender is not adequate,
please describe to the list your needs and we should be able to help suggest
possibly solutions other than extending Logger.


On May 7, 2006, at 7:49 PM, David Hosier wrote:

> Could anyone describe, or point me at resources that would describe, 
> the differences between creating your own Logger vs. creating your own 
> Appender for achieving custom logging behavior?  Thanks.
>
> -David
>
>
> ---------------------------------------------------------------------
> 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



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


Re: Logger vs. Appender

Posted by Curt Arnold <ca...@apache.org>.
An appender corresponds to the destination of a logging request, like  
a file, email, database record, network packet, etc.  Creating a  
custom appender to support new types of destinations is a common  
practice and well supported.

A logger corresponds to a topic of a logging request, like requests  
related to database activity, security or a particular class.   
Creating custom logger implementations is not a common practice.  The  
typical motivation to create a custom logger implementation is to  
support additional levels.  In most cases, the desire for additional  
new levels is due to a misunderstanding of the role of logger names  
(to represent topics or intended audience).  If you feel that a  
custom Appender is not adequate, please describe to the list your  
needs and we should be able to help suggest possibly solutions other  
than extending Logger.


On May 7, 2006, at 7:49 PM, David Hosier wrote:

> Could anyone describe, or point me at resources that would  
> describe, the
> differences between creating your own Logger vs. creating your own  
> Appender
> for achieving custom logging behavior?  Thanks.
>
> -David
>
>
> ---------------------------------------------------------------------
> 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