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 Ian Huynh <ia...@hubspan.com> on 2004/03/24 21:28:45 UTC

Is there a 'buffered' logging concept? ie. buffering message prior to knowing the Category of a logger

In normal usage of Log4J, this is a standard practice

  Logger logger = Logger.getLogger("com.acme.someclass");

  if (logger.isDebugEnabled()) 
  {
     logger.debug("This is my debug message" );
  }


What if the Category (ie  'com.acme.someclass') isn't a static value but some
dynamic value ? One typical example would be an application that needs to parse some request and
within the request, there are some contextual information that defines the Category name ?

So prior to knowing whether the Category of a logger is debug or infor etc.., we still like to
buffer some messages and then once the Category can be defined, the debug messages can be flushed out
or throw away.

eg. 

   Get a 'Buffer' Logger instance
   Get Request from Client
   logDebug("Got request from Client. about to parse")
   
   parsedRequest = ParseRequest()

   logDebug("Request Parsed")

   String contextInfo = Get from parsedRequest 

   Re-get the Log4j logger using contextInfo as category (ie   newLogger= Logger.getLogger( contextInfo ) )

   if ( newLogger is debug enabled)
      flush all the previous messages to the appender

I am curious if anyone has similar requirements or have implememented something similar.  I've thought
about writing a custom BufferLogger but would rather see what else is out there.

Thanks in advance.

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


Re: Is there a 'buffered' logging concept? ie. buffering message prior to knowing the Category of a logger

Posted by Charles Tuckey <ct...@verano.com>.
Hi,

I had this problem with an application we are developing as well. Prior 
to setting up the logging a number of initialization steps are performed 
which may result in loggin messages being generated. I didn't want to 
print these until I knew whether they should be printed and how they 
should be formatted. Here is some of my actual code with a bit of 
verbage. Let me know if you need or would like more info.


I created a custom Appender:

     public class StoreAppender extends AppenderSkeleton {

         ...
         ...
         ...

         public void dumpEvents() {
             // dump out the stored logging events
             Iterator iter = logEvents.iterator();
             while (iter.hasNext()) {
                 LoggingEvent event = (LoggingEvent) iter.next();
                 Logger logger = Logger.getLogger(event.getLoggerName());
                 if ( 
event.getLevel().isGreaterOrEqual(logger.getEffectiveLevel()) ) {
                     logger.callAppenders(event);
                 }
             }

         }   // dumpEvents

     }

In my main class I instantiated it and set up a basic configurator that 
used it. This was what is used until logging is set up.

     private static StoreAppender tempAppender = new StoreAppender();

     static {
         // setup a basic logging configuration to be used until we can 
read
         // the logger properties file
         BasicConfigurator.configure(tempAppender);
     }


Here I set up the logging and dump out the log messages. If the program 
exits before this point it is still possible to print out any log 
messages that have been saved in StoreAppender (which is handy).

    /**
      * Initializes the log4j logging system.
      */
     private void setupLogging() {
         logger.info("Reading logger configuration from " + 
Utils.getURLSpecification(loggerPropertiesURL));

         LogManager.resetConfiguration();
         PropertyConfigurator.configure(loggerPropertiesURL);

         // have to assume appenders were setup properly since there is 
no programmatic way to check
         tempAppender.dumpEvents();
         tempAppender.dispose();
         tempAppender = null;

     } // setupLogging


charlie

Ian Huynh wrote:
> In normal usage of Log4J, this is a standard practice
> 
>   Logger logger = Logger.getLogger("com.acme.someclass");
> 
>   if (logger.isDebugEnabled()) 
>   {
>      logger.debug("This is my debug message" );
>   }
> 
> 
> What if the Category (ie  'com.acme.someclass') isn't a static value but some
> dynamic value ? One typical example would be an application that needs to parse some request and
> within the request, there are some contextual information that defines the Category name ?
> 
> So prior to knowing whether the Category of a logger is debug or infor etc.., we still like to
> buffer some messages and then once the Category can be defined, the debug messages can be flushed out
> or throw away.
> 
> eg. 
> 
>    Get a 'Buffer' Logger instance
>    Get Request from Client
>    logDebug("Got request from Client. about to parse")
>    
>    parsedRequest = ParseRequest()
> 
>    logDebug("Request Parsed")
> 
>    String contextInfo = Get from parsedRequest 
> 
>    Re-get the Log4j logger using contextInfo as category (ie   newLogger= Logger.getLogger( contextInfo ) )
> 
>    if ( newLogger is debug enabled)
>       flush all the previous messages to the appender
> 
> I am curious if anyone has similar requirements or have implememented something similar.  I've thought
> about writing a custom BufferLogger but would rather see what else is out there.
> 
> Thanks in advance.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 

-- 
Charles Tuckey, MSc.
Software Developer
Verano, Inc.
403.299.4765

  © Copyright 2004 Verano Inc. owns copyright content of this document 
and all attachments unless otherwise indicated. All rights reserved. 
Users of Verano Inc. software and tools associated with the software 
such as sales & marketing collateral, presentations, user manuals, 
training documentation etc. may not republish nor reproduce in whole or 
in part the information, in any form or by any means, in any manner 
whatsoever without the prior written permission of Verano Inc., and any 
such unauthorized use constitutes copyright infringement. An 
acknowledgement of the source must be included whenever Verano Inc. 
material is copied or published. If you require further information on a 
permitted use or license to reproduce or republish any material, address 
your inquiry to Verano Inc.Suite 120, 575 West Street, Mansfield, 
Massachusetts, 02048-1164. Any infringement of Verano Inc. rights  will 
result in appropriate legal action. Verano Inc. disclaims any and all 
liability for any consequences which may result from any unauthorized 
reproduction or use of this Work whatsoever.



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