[logback-dev] [Bug 141] New: introduce FlushableAppender

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Tue Mar 25 19:56:48 CET 2008


http://bugzilla.qos.ch/show_bug.cgi?id=141

           Summary: introduce FlushableAppender
           Product: logback-core
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: Appender
        AssignedTo: logback-dev at qos.ch
        ReportedBy: bruno.navert at morganstanley.com


Suggest a new sub-interface of Appender:

public interface FlushableAppender<E> extends Appender<E>, java.io.Flushable
{
}

Then, WriterAppender could be defined to implement FlushableAppender, with this
simple implementation:

    public void flush() throws IOException
    {
        writer.flush();
    }

This would allow manual flushing of the appenders. This is particularly useful
when buffered IO is used, obviously. It allows, for instance, to manually flush
all appenders when a request has been fully processed, ensuring that we retain
the benefits of buffered IO while also having the full logs after request
processing.

Here's sample code I used to get all appenders (run once after Logback
configuration):

    public static Set<Appender> getAllAppenders()
    {
        ContextSelector selector =
StaticLoggerBinder.SINGLETON.getContextSelector();
        LoggerContext loggerContext = selector.getLoggerContext();

        Map<String, Appender> appenders = newHashMap();

        // loop through all Loggers
        for ( Logger logger : loggerContext.getLoggerList() )
        {
            // for each logger, loop through all its appenders
            Iterator iter = logger.iteratorForAppenders();
            while ( iter.hasNext() )
            {
                // appenders are uniquely identified by name, so store them in
the Map thus
                // this will overwrite the same entry in the Map many times
(with the same reference)
                Appender appender = ( Appender ) iter.next();
                appenders.put( appender.getName(), appender );
            }
        }

        return newHashSet( appenders.values() );
    }

The below bean is used in Spring, calling flush() forces all appenders to be
flushed:

public class LogbackFlushBean implements Flushable
{
    protected final Logger log = LoggerFactory.getLogger( getClass() );

    private final Collection<FlushableAppender> flushableAppenders =
newLinkedList();

    @PostConstruct
    public void loadFlushableAppenders()
    {
        for ( Appender appender : LogbackConfigurer.getAllAppenders() )
        {
            if ( appender instanceof FlushableAppender )
            {
                flushableAppenders.add( ( FlushableAppender ) appender );
            }
            else
            {
                log.debug( "appender {} is not Flushable, skipping",
appender.getName() );
            }
        }
    }

    public void flush() throws IOException
    {
        for ( FlushableAppender appender : flushableAppenders )
        {
            log.debug( "flushing appender {}", appender.getName() );
            appender.flush();
        }
    }
}


-- 
Configure bugmail: http://bugzilla.qos.ch/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the logback-dev mailing list