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 Sloan Seaman <sl...@sgi.net> on 2004/01/27 19:19:46 UTC

Listening for events

I have an object A, that uses log4j for various logging.

I have another object B, that I want to listen for any logging events within
A that are warning, errors, or fatals.

How do I registers B so that it will be informed when A does a log message?

Thanks!

--
Sloan


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
http://www.messagelabs.com
________________________________________________________________________

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


Re: Listening for events

Posted by Paul Smith <pa...@lawlex.com.au>.
On Wed, 2004-01-28 at 05:19, Sloan Seaman wrote:
> I have an object A, that uses log4j for various logging.
> 
> I have another object B, that I want to listen for any logging events within
> A that are warning, errors, or fatals.
> 
> How do I registers B so that it will be informed when A does a log message?

This should be straight forward.  Make B either extend from, or have an
inner-class that extends from the AppenderSkeleton class, and make B
register an instance of that inner class (or itself) as an appender on
the logger that A is is logging to.  That way B's inner class will
receive all the LoggingEvents objects for the logger A is logging to.

e.g.

class A{
 private static Logger LOG = Logger.getLogger(A.class);

 public void doSomething(){
  LOG.info("Hi there B!");
 }
}
......

class B{
 private BLogListener listener = new BLogListener();

 public B() {
  // register the internal ristener as an appender
   Logger.getLogger(A.class).addAppender(listener);
 }

 private void notifyBThatALoggedSomething(LoggingEvent e) {
  ....
 }


 private class BLogListener extends AppenderSkeleton {
   
  protected abstract void append(LoggingEvent event){
    notifyBThatALoggedSomething(event);
  }
 }
}


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