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 Rohit Sonalkar <ro...@ipunity.com> on 2005/04/15 05:30:51 UTC

Sequence of logging events

Hi,

 

Is it possible in a multi-threaded application that log statements
appear in the output log file out of sequence w.r.t. the timestamps of
the logging events? If the timestamp is generated at the time the
logging method is invoked, then under what conditions would this happen?
Would it happen if a logger generated a logging event, but by the time
it found its way to its parent categories' appender through additivity,
the logger directly attached to that appender gets in a logging event? 

 

Thanks

Rohit

 


Re: Sequence of logging events

Posted by Ceki Gülcü <ce...@qos.ch>.
At 05:30 AM 4/15/2005, Rohit Sonalkar wrote:
>Hi,
>
>Is it possible in a multi-threaded application that log statements
>appear in the output log file out of sequence w.r.t. the timestamps of
>the logging events? If the timestamp is generated at the time the
>logging method is invoked, then under what conditions would this happen?
>Would it happen if a logger generated a logging event, but by the time
>it found its way to its parent categories' appender through additivity,
>the logger directly attached to that appender gets in a logging event?


I believe the short answer to be 'yes'. The longer answer follows.

The creation of the logging event which contains the time stamp and
actual writing of the event through the appenders is not
atomic. However, all the appenders attached to a given logger are
invoked atomically.

Thus, it possible for the time stamps of distinct events to be written
in different order through appender A and appender B but only if
appenders A and B are attached to distinct loggers. If A and B are
attached to the same logger, then all events output through appender A
will be output exactly in the same order in B, provided that both A
and B log the event.

Although related, there is also the separate question mentioned by
Curt, that is the order in which events occur and the order in which
they are output by log4j as epitomized by the AsyncAppender reordering
problem when its buffer is full.

HTH,


>Thanks
>
>Rohit

-- 
Ceki Gülcü

   The complete log4j manual: http://www.qos.ch/log4j/



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


Re: Sequence of logging events

Posted by Curt Arnold <ca...@apache.org>.
On Apr 14, 2005, at 10:30 PM, Rohit Sonalkar wrote:

> Hi,
>
> Is it possible in a multi-threaded application that log statements
> appear in the output log file out of sequence w.r.t. the timestamps of
> the logging events? If the timestamp is generated at the time the
> logging method is invoked, then under what conditions would this 
> happen?
> Would it happen if a logger generated a logging event, but by the time
> it found its way to its parent categories' appender through additivity,
> the logger directly attached to that appender gets in a logging event?
>
>

Yes.  The time stamp is generated at the construction of the 
LoggingEvent which occurs during the call to Logger.log after checking 
the thresholds but before dispatching the LoggingEvent to the 
appropriate appenders.  The appenders have synchronization blocks and 
if multiple threads are waiting for a lock, the Java VM makes no 
promises on the order that the waiting threads will get the lock.

If while event 1 is being processed by appender 1 on thread 1, threads 
2 and 3 make log requests and block due to appender 1 when event 1 is 
finished processing, either event 2 or 3 may be next.  The effect is 
most noticeable with AsyncAppenders which will block when their buffer 
is filled.

These scenarios have been discussed in depth on the log4j-dev list 
(search for sequenceNumber and be ready to read 30 messages to follow 
the ious positions).  There does not appear to be any simple solution 
other than making your AsyncAppender big enough that you don't block 
and accept that if you are multi-threaded that timestamps may still 
appear out of sequence.  Changing the timestamps so that they reflect 
the time of appending instead of the time of request would remove the 
cosmetic issues, however it would  disguise the true sequence of the 
requests.


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