Class MemoryAppender

java.lang.Object
org.apache.logging.log4j.core.AbstractLifeCycle
org.apache.logging.log4j.core.filter.AbstractFilterable
org.apache.logging.log4j.core.appender.AbstractAppender
com.randomnoun.common.log4j2.MemoryAppender
All Implemented Interfaces:
org.apache.logging.log4j.core.Appender, org.apache.logging.log4j.core.filter.Filterable, org.apache.logging.log4j.core.impl.LocationAware, org.apache.logging.log4j.core.LifeCycle, org.apache.logging.log4j.core.LifeCycle2

@Plugin(name="Memory", category="Core", elementType="appender", printObject=true) public class MemoryAppender extends org.apache.logging.log4j.core.appender.AbstractAppender
Log4j2 appender to capture logging events in an in-memory List.

Maybe log4j2 already has one. That'd be nice.

Apparently not.

The code in this class is based on the WriterAppender class in the log4j source code.

This appender can be configured using the property "maximumLogSize" which limits the number of logging events captured by this class (old events are popped off the list when the list becomes full).

Use the getLogEvents() to return the List of events written to this class. This list is a copy of the list contained within this class, so it can safely be iterated over even if logging events are still occurring in an application.

Example log4j configuration:

 log4j.rootLogger=DEBUG, MEMORY
 
 log4j.appender.MEMORY=com.randomnoun.common.log4j.MemoryAppender
 log4j.appender.MEMORY.MaximumLogSize=1000
 
You can then obtain the list of events via the code:
 MemoryAppender memoryAppender = (MemoryAppender) Logger.getRootLogger().getAppender("MEMORY");
 List events = memoryAppender.getEvents();
 
Author:
knoxg
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builds HttpAppender instances.

    Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B extends org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B>>

    Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle

    org.apache.logging.log4j.core.LifeCycle.State
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final String
     

    Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER

    Fields inherited from interface org.apache.logging.log4j.core.Appender

    ELEMENT_TYPE, EMPTY_ARRAY
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(org.apache.logging.log4j.core.LogEvent event)
     
     
    List<org.apache.logging.log4j.core.LogEvent>
    Returns a list of logging events captured by this appender.
    int
    Return the maximum log size
     
    void
    setMaximumLogSize(int logSize)
    Set the maximum log size

    Methods inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    error, error, error, getHandler, getLayout, getName, ignoreExceptions, parseInt, requiresLocation, setHandler, toSerializable, toString

    Methods inherited from class org.apache.logging.log4j.core.filter.AbstractFilterable

    addFilter, getFilter, getPropertyArray, hasFilter, isFiltered, removeFilter, start, stop, stop

    Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.apache.logging.log4j.core.LifeCycle

    getState, initialize, isStarted, isStopped, start, stop