Package org.apache.log4j
Class AsyncAppender
java.lang.Object
org.apache.log4j.AppenderSkeleton
org.apache.log4j.AsyncAppender
- All Implemented Interfaces:
Appender,AppenderAttachable,OptionHandler
The AsyncAppender lets users log events asynchronously.
The AsyncAppender will collect the events sent to it and then dispatch them to all the appenders that are attached to
it. You can attach multiple appenders to an AsyncAppender.
The AsyncAppender uses a separate thread to serve the events in its buffer.
Important note: The
AsyncAppender can only be script
configured using the DOMConfigurator.
- Since:
- 0.9.1
- Author:
- Ceki Gülcü, Curt Arnold
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default buffer size is set to 128 events.Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAppender(Appender newAppender) Add appender.voidappend(LoggingEvent event) Subclasses ofAppenderSkeletonshould implement this method to perform actual logging.voidclose()Close thisAsyncAppenderby interrupting the dispatcher thread which will process all pending events before exiting.Get iterator over attached appenders.getAppender(String name) Get appender by name.booleanGets whether appender should block calling thread when buffer is full.intGets the current buffer size.booleanGets whether the location of the logging request call should be captured.booleanisAttached(Appender appender) Determines if specified appender is attached.voidRemoves and closes all attached appenders.voidremoveAppender(String name) Remove appender by name.voidremoveAppender(Appender appender) Removes an appender.booleanConfigurators call this method to determine if the appender requires a layout.voidsetBlocking(boolean value) Sets whether appender should wait if there is no space available in the event buffer or immediately return.voidsetBufferSize(int size) Sets the number of messages allowed in the event buffer before the calling thread is blocked (if blocking is true) or until messages are summarized and discarded.voidsetLocationInfo(boolean flag) The LocationInfo option takes a boolean value.Methods inherited from class org.apache.log4j.AppenderSkeleton
activateOptions, addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEThe default buffer size is set to 128 events.- See Also:
-
-
Constructor Details
-
AsyncAppender
public AsyncAppender()Create new instance.
-
-
Method Details
-
addAppender
Add appender.- Specified by:
addAppenderin interfaceAppenderAttachable- Parameters:
newAppender- appender to add, may not be null.
-
append
Subclasses ofAppenderSkeletonshould implement this method to perform actual logging. See alsoAppenderSkeleton.doAppendmethod.- Specified by:
appendin classAppenderSkeleton
-
close
public void close()Close thisAsyncAppenderby interrupting the dispatcher thread which will process all pending events before exiting. -
getAllAppenders
Get iterator over attached appenders.- Specified by:
getAllAppendersin interfaceAppenderAttachable- Returns:
- iterator or null if no attached appenders.
-
getAppender
Get appender by name.- Specified by:
getAppenderin interfaceAppenderAttachable- Parameters:
name- name, may not be null.- Returns:
- matching appender or null.
-
getLocationInfo
public boolean getLocationInfo()Gets whether the location of the logging request call should be captured.- Returns:
- the current value of the LocationInfo option.
-
isAttached
Determines if specified appender is attached.- Specified by:
isAttachedin interfaceAppenderAttachable- Parameters:
appender- appender.- Returns:
- true if attached.
-
requiresLayout
public boolean requiresLayout()Configurators call this method to determine if the appender requires a layout. If this method returnstrue, meaning that layout is required, then the configurator will configure an layout using the configuration information at its disposal. If this method returnsfalse, meaning that a layout is not required, then layout configuration will be skipped even if there is available layout configuration information at the disposal of the configurator..In the rather exceptional case, where the appender implementation admits a layout but can also work without it, then the appender should return
true.- Specified by:
requiresLayoutin interfaceAppender
-
removeAllAppenders
public void removeAllAppenders()Removes and closes all attached appenders.- Specified by:
removeAllAppendersin interfaceAppenderAttachable
-
removeAppender
Removes an appender.- Specified by:
removeAppenderin interfaceAppenderAttachable- Parameters:
appender- appender to remove.
-
removeAppender
Remove appender by name.- Specified by:
removeAppenderin interfaceAppenderAttachable- Parameters:
name- name.
-
setLocationInfo
public void setLocationInfo(boolean flag) The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no effort to extract the location information related to the event. As a result, the event that will be ultimately logged will likely to contain the wrong location information (if present in the log format). Location information extraction is comparatively very slow and should be avoided unless performance is not a concern.- Parameters:
flag- true if location information should be extracted.
-
setBufferSize
public void setBufferSize(int size) Sets the number of messages allowed in the event buffer before the calling thread is blocked (if blocking is true) or until messages are summarized and discarded. Changing the size will not affect messages already in the buffer.- Parameters:
size- buffer size, must be positive.
-
getBufferSize
public int getBufferSize()Gets the current buffer size.- Returns:
- the current value of the BufferSize option.
-
setBlocking
public void setBlocking(boolean value) Sets whether appender should wait if there is no space available in the event buffer or immediately return.- Parameters:
value- true if appender should wait until available space in buffer.- Since:
- 1.2.14
-
getBlocking
public boolean getBlocking()Gets whether appender should block calling thread when buffer is full. If false, messages will be counted by logger and a summary message appended after the contents of the buffer have been appended.- Returns:
- true if calling thread will be blocked when buffer is full.
- Since:
- 1.2.14
-