[logback-user] Dumping a CyclicBuffer

mweeda mweeda at 4idc.com
Wed Oct 15 23:09:35 CEST 2008

Thanks for your help, Ceki.

The attached files will show you what I came up with. Synchronization issues
are avoided by having the same Appender add events to the CyclicBuffer and
dump its contents. Ease of use is pretty high, since log.error(message)
triggers a buffer dump. Configuration changes would allow log.X and higher
to trigger a dump.

-----Original Message-----
From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On
Behalf Of Ceki Gulcu
Sent: Thursday, October 02, 2008 3:13 AM
To: logback users list
Subject: Re: [logback-user] Dumping a CyclicBuffer

Hello Mike,

Comments inline.

mweeda wrote:
> Hi,
> Objective: Dump a "history" of recent program operations to a log file
> an Exception arises. By logging to a CyclicBufferAppender, a recent
> at a fairly granular level (INFO, or possibly DEBUG) can be maintained
> without accumulating massive amounts of data, and with a minimum of
> overhead.

How do you link throwing an exception with dumping log buffer? Do you add
code that link the exception with dumping the log buffer into many try/catch

blocks manually? That sounds quite intrusive. Have you come up with
something else?

> Setting this up to log to the Cyclical Buffer went well. To get the data
> of the CyclicBuffer and log it to a file, a reference to the
> CyclicBufferAppender object is needed, and it is not clear how to get one.
> It seems like log.getAppender(String name) should return an Appender, but
> doesn't. How can I obtain such a reference?

In the config file you show, the appender named MEMORY is attached to the
logger. So you would need to write:

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.read.CyclicBufferAppender;

public class Main {

   public static void main(String[] args) {

     // The cast will work only if SLF4J is bound with logback
     Logger root = (Logger)

     CyclicBufferAppender<LoggingEvent> cba = 
(CyclicBufferAppender<LoggingEvent>) root.getAppender("MEMORY");

     // some form of synchronization is needed here!!!
     for(int i = 0; i < cba.getLength(); i++) {

> Also, once a reference is obtained, and an Object is returned from
> cyclicBufferAppender.get(i), what class will that object be? I'm hoping it
> is a LoggingEvent object, 

Yep, that is the case.

> in which case a simple call to the
> fileAppender.doAppend method with the event as a parameter will log the
> event to a "permanent" log. Again, the code will need to obtain a
> to the appropriate FileAppender to log it to. (Appropriate might be
> hardcoded, or specified in a properties file.)

You could configure a file appender and attach it to the cyclic buffer if
wish. Joran is designed to handle such use cases. Please let me know if you
to pursue this route.

> Most of the configuration is being done in logback.xml. (File is
> Program code, other than wanting to dump the MEMORY appender, is limited
> static final Logger log = LoggerFactory.getLogger(Main.class);
> and various log.X method calls.
> As an aside, it would be even better to loop over all Appenders, and check
> to see which ones are CyclicBufferAppenders. The code would dump then
> contents of all of the CyclicBufferAppenders, or selected ones based on
> user options. 

How about the following code?

     LoggerContext context = (LoggerContext)
     for (Logger l : context.getLoggerList()) {
        Iterator ite = l.iteratorForAppenders();
        while(ite.hasNext()) {
          Object o = ite.next();
          if(o instanceof CyclicBufferAppender) {
            // dump

> Mike
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for
Logback-user mailing list
Logback-user at qos.ch
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Main.java
Type: java/*
Size: 1191 bytes
Desc: not available
Url : http://qos.ch/pipermail/logback-user/attachments/20081015/84286121/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ExcludeRangeFilter.java
Type: java/*
Size: 2305 bytes
Desc: not available
Url : http://qos.ch/pipermail/logback-user/attachments/20081015/84286121/attachment-0001.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CyclicBufferDumpAppender.java
Type: java/*
Size: 4300 bytes
Desc: not available
Url : http://qos.ch/pipermail/logback-user/attachments/20081015/84286121/attachment-0002.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: logback.xml
Type: text/xml
Size: 6008 bytes
Desc: not available
Url : http://qos.ch/pipermail/logback-user/attachments/20081015/84286121/attachment-0003.bin 

More information about the Logback-user mailing list