[logback-user] Dumping a CyclicBuffer

Ceki Gulcu listid at qos.ch
Thu Oct 2 10:12:59 CEST 2008

Hello Mike,

Comments inline.

mweeda wrote:
> Hi,
> Objective: Dump a "history" of recent program operations to a log file when
> an Exception arises. By logging to a CyclicBufferAppender, a recent history
> 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 the 
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 out
> 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 it
> doesn't. How can I obtain such a reference?

In the config file you show, the appender named MEMORY is attached to the root 
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) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

     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 reference
> 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 you 
wish. Joran is designed to handle such use cases. Please let me know if you want 
to pursue this route.

> Most of the configuration is being done in logback.xml. (File is attached.)
> Program code, other than wanting to dump the MEMORY appender, is limited to:
> 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 some
> user options. 

How about the following code?

     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
     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 Java.

More information about the Logback-user mailing list