logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ceki Gülcü <c...@qos.ch>
Subject Re: Yet another Apender proposal: ListAppender
Date Mon, 18 Oct 2004 14:57:06 GMT

Hi Michael,

Joran also requires a buffer type appender to collect logging events that 
it generates while configuring the log4j environment. Once the config file 
is parsed, it instructs the buffer to forward the collected events in the 
new environment. This sounds similar to the motivation you had.

In any case, thanks for your contribution,

At 10:32 PM 10/15/2004, spam2004@meeque.de wrote:
>Hello folks!
>
>Probably this is getting too frequent now, but I'd like to propose a new
>Appender, too. I guess it is not so sophisticated and usefull as the
>MultiplexAppender proposed recently, but therefore it is small and
>simple. It's only one class, so I've simply attached it to this mail.
>The Javadoc coments are rather low quality, but they should give a good
>idea about how it works.
>
>My motivation was, to store some LoggingEvents in my application, so
>that they could be presented in its GUI later. In particular, was
>writing some complex import filter, and was logging all import steps
>with Log4j. But I also wanted to give the user some direct feedback
>about the progress. Instead of accumulating messages for the GUI
>seperately, I decided to use the existing Log4j infrastructure for this.
>This would reduce the additional logging code in the import.
>
>However the GUI is asynchronous to the import, so I needed to store the
>LoggingEvents somewhere, and retrive them, whenever they should be
>rendered. Of course, I could have used some Appender writing to a file,
>and parse that file to display the events. But in fact the GUI didn't
>need to make the events persistent, it just needed them in memory. So I
>wrote that simple ListAppender, which buffers the LoggingEvents in a
>List. The application using this Appender has full control of the List,
>and can retrive or modify the stored LoggingEvent whenever it wants.
>
>I could't find any similar Appender in the Log4j API docs. So either
>you're in need of it, or it is completely unusefull for anyone else.
>Please have a look at it, and decide yourself. If you think any of the
>code could be usefull for Log4j, please feel free to use it.
>
>Regards,
>Michael
>
>PS: Thanks for all the other Appenders and Log4j in general. It's been
>very usefull for me, so far.
>
>
>package org.apache.log4j;
>
>import java.util.Collections;
>import java.util.LinkedList;
>import java.util.List;
>
>import org.apache.log4j.spi.LoggingEvent;
>
>/**
>  * A log4j Appender, which stores LoggingEvents received in a list.
>  * The stored LoggingEvents can be accessed later for further processing.
>  */
>public class ListAppender extends AppenderSkeleton {
>
>    private static final Logger LOG = Logger.getLogger(ListAppender.class);
>
>    /** */
>    private final List loggedEvents;
>
>    /**
>     * Creates a new ListAppender.
>     * Its List of LoggingEvents is an LinkedList and is initially empty.
>     */
>    public ListAppender() {
>       loggedEvents = new LinkedList();
>    }
>
>    /**
>     * Creates a new ListAppender.
>     * The given List will be used as List of LoggingEvents and will 
> initially be cleared.
>     * Use this Constructor if you need something else than the 
> unsynchronized LinkedList used by default.
>     * @param A modifiable List to append LoggingEvents to.
>     */
>    public ListAppender(List list) {
>       loggedEvents = list;
>       loggedEvents.clear();
>    }
>
>    /**
>     * Appends a LoggingEvent to this Appender's List of LoggingEvents.
>     * It is usually called by the doAppend method of its super-class.
>     * @param logEvent The LoggingEvent to append.
>     */
>    protected void append(LoggingEvent logEvent) {
>       loggedEvents.add(logEvent);
>    }
>
>    /**
>     * Nothing needs to be done, when this Appender is closed.
>     */
>    public void close() {
>    }
>
>    /**
>     * This Appender does not require any layout. It does not format the 
> LoggingEvents.
>     * @return Always false.
>     */
>    public boolean requiresLayout() {
>       return false;
>    }
>
>    /**
>     * Gets this Appender's List of LoggingEvents.
>     * The List contains any LoggingEvents appended to this Appender
>     * since its creation or the last call to its clear method.
>     * @return An unmodifyable List of LoggingEvents.
>     */
>    public List getLoggedEvents() {
>       return Collections.unmodifiableList(loggedEvents);
>    }
>
>    /**
>     * Resets this Appender by clearing its List of LoggingEvents.
>     * Other LoggingEvents can be appended afterwards.
>     */
>    public void reset() {
>       loggedEvents.clear();
>    }
>
>}
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-dev-help@logging.apache.org

-- 
Ceki Gülcü

      For log4j documentation consider "The complete log4j manual"
      ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp  



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


Mime
View raw message