org.apache.log4j

Class AsyncAppender

Implemented Interfaces:
Appender, AppenderAttachable, OptionHandler

public class AsyncAppender
extends AppenderSkeleton
implements AppenderAttachable

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.

Authors:
Ceki Gülcü
Curt Arnold
Since:
0.9.1

Field Summary

static int
DEFAULT_BUFFER_SIZE
The 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

AsyncAppender()
Create new instance.

Method Summary

void
addAppender(Appender newAppender)
Add appender.
void
append(LoggingEvent event)
void
close()
Close this AsyncAppender by interrupting the dispatcher thread which will process all pending events before exiting.
Enumeration
getAllAppenders()
Get iterator over attached appenders.
Appender
getAppender(String name)
Get appender by name.
boolean
getBlocking()
Gets whether appender should block calling thread when buffer is full.
int
getBufferSize()
Gets the current buffer size.
boolean
getLocationInfo()
Gets whether the location of the logging request call should be captured.
boolean
isAttached(Appender appender)
Determines if specified appender is attached.
void
removeAllAppenders()
Removes and closes all attached appenders.
void
removeAppender(String name)
Remove appender by name.
void
removeAppender(Appender appender)
Removes an appender.
boolean
requiresLayout()
void
setBlocking(boolean value)
Sets whether appender should wait if there is no space available in the event buffer or immediately return.
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.
void
setLocationInfo(boolean flag)
The LocationInfo option takes a boolean value.

Methods inherited from class org.apache.log4j.AppenderSkeleton

activateOptions, addFilter, append, 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_SIZE
The default buffer size is set to 128 events.
Field Value:
128

Constructor Details

AsyncAppender

public AsyncAppender()
Create new instance.

Method Details

addAppender

public void addAppender(Appender newAppender)
Add appender.
Specified by:
addAppender in interface AppenderAttachable
Parameters:
newAppender - appender to add, may not be null.

append

public void append(LoggingEvent event)
Overrides:
append in interface AppenderSkeleton

close

public void close()
Close this AsyncAppender by interrupting the dispatcher thread which will process all pending events before exiting.
Specified by:
close in interface Appender

getAllAppenders

public Enumeration getAllAppenders()
Get iterator over attached appenders.
Specified by:
getAllAppenders in interface AppenderAttachable
Returns:
iterator or null if no attached appenders.

getAppender

public Appender getAppender(String name)
Get appender by name.
Specified by:
getAppender in interface AppenderAttachable
Parameters:
name - name, may not be null.
Returns:
matching appender or null.

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.

getBufferSize

public int getBufferSize()
Gets the current buffer size.
Returns:
the current value of the BufferSize option.

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

public boolean isAttached(Appender appender)
Determines if specified appender is attached.
Specified by:
isAttached in interface AppenderAttachable
Parameters:
appender - appender.
Returns:
true if attached.

removeAllAppenders

public void removeAllAppenders()
Removes and closes all attached appenders.
Specified by:
removeAllAppenders in interface AppenderAttachable

removeAppender

public void removeAppender(String name)
Remove appender by name.
Specified by:
removeAppender in interface AppenderAttachable
Parameters:
name - name.

removeAppender

public void removeAppender(Appender appender)
Removes an appender.
Specified by:
removeAppender in interface AppenderAttachable
Parameters:
appender - appender to remove.

requiresLayout

public boolean requiresLayout()
Specified by:
requiresLayout in interface Appender

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.

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.

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.

Copyright 2000-2005 Apache Software Foundation.