Package com.randomnoun.common.log4j2
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=1000You 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 ClassesModifier and TypeClassDescriptionstatic 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
FieldsFields 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 TypeMethodDescriptionvoid
append
(org.apache.logging.log4j.core.LogEvent event) static MemoryAppender
createAppender
(String name) List
<org.apache.logging.log4j.core.LogEvent> Returns a list of logging events captured by this appender.int
Return the maximum log sizestatic <B extends MemoryAppender.MemoryAppenderBuilder<B>>
Bvoid
setMaximumLogSize
(int logSize) Set the maximum log sizeMethods 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
-
Field Details
-
PLUGIN_NAME
- See Also:
-
DEFAULT_LOG_SIZE
- See Also:
-
-
Method Details
-
newBuilder
@PluginBuilderFactory public static <B extends MemoryAppender.MemoryAppenderBuilder<B>> B newBuilder()- Returns:
- a builder for a MemoryAppender.
-
createAppender
@PluginFactory public static MemoryAppender createAppender(@PluginAttribute(value="name",defaultString="null") String name) -
setMaximumLogSize
Set the maximum log size -
getMaximumLogSize
Return the maximum log size -
append
-
getLogEvents
Returns a list of logging events captured by this appender. (This list is cloned in order to prevent ConcurrentModificationExceptions occuring whilst iterating through it)
-