Apache Log4cxx  Version 1.5.0
Loading...
Searching...
No Matches
log4cxx::AsyncAppender Class Reference

The AsyncAppender decouples logging event creation from output by processing log events asynchronously. More...

#include <asyncappender.h>

Inheritance diagram for log4cxx::AsyncAppender:
log4cxx::spi::AppenderAttachable log4cxx::AppenderSkeleton log4cxx::helpers::Object log4cxx::Appender log4cxx::helpers::Object log4cxx::spi::OptionHandler log4cxx::helpers::Object

Public Member Functions

 AsyncAppender ()
 Create new instance.
virtual ~AsyncAppender ()
 Destructor.
void addAppender (const AppenderPtr newAppender) override
 Ensure newAppender receives any logging event added to this appender.
void doAppend (const spi::LoggingEventPtr &event, helpers::Pool &pool1) override
 This method performs threshold checks and invokes filters before delegating actual logging to the subclasses specific AppenderSkeleton::append method.
void append (const spi::LoggingEventPtr &event, helpers::Pool &p) override
 Subclasses of AppenderSkeleton should implement this method to perform actual logging.
void close () override
 Close this AsyncAppender by interrupting the dispatcher thread which will process all pending events before exiting.
AppenderList getAllAppenders () const override
 Get iterator over attached appenders.
AppenderPtr getAppender (const LogString &name) const override
 Get appender by name.
bool getLocationInfo () const
 The current value of the (unused) LocationInfo option.
bool isAttached (const AppenderPtr appender) const override
 Determines if specified appender is attached.
bool requiresLayout () const override
 Configurators call this method to determine if the appender requires a layout.
void removeAllAppenders () override
 Removes and closes all attached appenders.
void removeAppender (const AppenderPtr appender) override
 Removes an appender.
void removeAppender (const LogString &name) override
 Remove appender by name.
bool replaceAppender (const AppenderPtr &oldAppender, const AppenderPtr &newAppender) LOG4CXX_16_VIRTUAL_SPECIFIER
 Replace oldAppender with newAppender.
void replaceAppenders (const AppenderList &newList) LOG4CXX_16_VIRTUAL_SPECIFIER
 Replace any previously added appenders with newList.
void setLocationInfo (bool flag)
 The LocationInfo attribute is provided for compatibility with log4j and has no effect on the log output.
void setBufferSize (int size)
 The BufferSize option takes a non-negative integer value.
int getBufferSize () const
 Gets the current buffer size.
void setBlocking (bool value)
 Sets whether appender should wait if there is no space available in the event buffer or immediately return.
bool getBlocking () const
 Gets whether appender should block calling thread when buffer is full.
void setOption (const LogString &option, const LogString &value) override
 Set option to value.
Public Member Functions inherited from log4cxx::spi::AppenderAttachable
virtual ~AppenderAttachable ()
Public Member Functions inherited from log4cxx::helpers::Object
virtual ~Object ()
virtual const helpers::ClassgetClass () const =0
virtual bool instanceof (const Class &clazz) const =0
virtual const void * cast (const Class &clazz) const =0
Public Member Functions inherited from log4cxx::AppenderSkeleton
 AppenderSkeleton ()
 AppenderSkeleton (const LayoutPtr &layout)
virtual ~AppenderSkeleton ()
void finalize ()
 Finalize this appender by calling the derived class' close method.
void activateOptions (helpers::Pool &) override
 Activate the options that were previously set with calls to option setters.
void addFilter (const spi::FilterPtr newFilter) override
 Add a filter to end of the filter list.
void clearFilters () override
 Clear the filters chain.
const spi::ErrorHandlerPtr getErrorHandler () const
 Return the currently set spi::ErrorHandler for this Appender.
spi::FilterPtr getFilter () const override
 Returns the head Filter.
const spi::FilterPtr getFirstFilter () const
 Return the first filter in the filter chain for this Appender.
LayoutPtr getLayout () const override
 Returns the layout of this appender.
LogString getName () const override
 Returns the name of this Appender.
const LevelPtr getThreshold () const
 Returns this appenders threshold level.
bool isAsSevereAsThreshold (const LevelPtr &level) const
 Check whether the message level is below the appender's threshold.
void setErrorHandler (const spi::ErrorHandlerPtr eh)
 Set the ErrorHandler for this Appender.
void setLayout (const LayoutPtr layout1) override
 Set the layout for this appender.
void setName (const LogString &name1) override
 Set the name of this Appender.
void setThreshold (const LevelPtr &threshold)
 Set the threshold level.
Public Member Functions inherited from log4cxx::Appender
virtual ~Appender ()
Public Member Functions inherited from log4cxx::spi::OptionHandler
virtual ~OptionHandler ()

Additional Inherited Members

Protected Member Functions inherited from log4cxx::AppenderSkeleton
 AppenderSkeleton (LOG4CXX_PRIVATE_PTR(AppenderSkeletonPrivate) priv)
void doAppendImpl (const spi::LoggingEventPtr &event, log4cxx::helpers::Pool &pool)

Detailed Description

The AsyncAppender decouples logging event creation from output by processing log events asynchronously.

The AsyncAppender stores the logging event in a bounded buffer and then returns control to the application. A separate thread forwards events to the attached appender(s).

Important notes:

  • Your application must call LogManager::shutdown when it exits to prevent undefined behaviour when using this appender.
  • Runtime configuration requires an XML configuration file (see the example below).

This appender is useful when outputting to a slow event sink, for example, a remote SMTP server or a database. Attaching a FileAppender to AsyncAppender to reduce logging overhead is not recommended as the inter-thread communication overhead can exceed the time to write directly to a file.

You can attach multiple appenders to an AsyncAppender by:

  • calling AsyncAppender::addAppender repeatedly when progammatically configuring Log4cxx.
  • adding multiple appender-ref elements to the appender class="AsyncAppender" element when using runtime configuration.

Here is a sample configuration file:

<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="A1" class="RollingFileAppender">
<param name="File" value="${TEMP}/SomeApplicationName.log" />
<param name="Append" value="true" />
<layout class="PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n"/>
</layout>
</appender>
<appender name="SENDMAIL" class="SMTPAppender">
<param name="from" value="service_name@example.org" />
<param name="to" value="some_support_group@example.org" />
<param name="subject" value="Service error detected" />
<param name="SMTPHost" value="smtp.example.com"/>
<layout class="PatternLayout">
<param name="ConversionPattern" value="%-5p %c{2} - %m%n"/>
</layout>
<!-- triggeringPolicy class="SpecialTriggeringEventEvaluator" -->
<!-- param name="evaluatorClass" value="SpecialTriggeringEventEvaluator" -->
</appender>
<appender name="ASYNC" class="AsyncAppender">
<param name="BufferSize" value="1000"/>
<param name="Blocking" value="false"/>
<appender-ref ref="SENDMAIL"/>
</appender>
<root>
<priority value ="INFO" />
<appender-ref ref="A1" />
<appender-ref ref="ASYNC" />
</root>
</log4j:configuration>

Configurable properties

When the application produces logging events faster than the background thread is able to process, the bounded buffer can become full. In this situation AsyncAppender will either block until the bounded buffer has a free slot or discard the event. The Blocking property controls which behaviour is used. When events are discarded, the logged output will indicate this with a log message prefixed with Discarded. The output may contain one Discarded message per logger name, the logging event of the highest level for each logger whose events have been discarded.

To determine whether the application produces logging events faster than the background thread is able to process, enable Log4cxx internal debugging. The AsyncAppender will output a histogram of queue length frequencies when closed.

See AsyncAppender::setOption for details.

Constructor & Destructor Documentation

◆ AsyncAppender()

log4cxx::AsyncAppender::AsyncAppender ( )

Create new instance.

◆ ~AsyncAppender()

virtual log4cxx::AsyncAppender::~AsyncAppender ( )
virtual

Destructor.

Member Function Documentation

◆ addAppender()

void log4cxx::AsyncAppender::addAppender ( const AppenderPtr newAppender)
overridevirtual

Ensure newAppender receives any logging event added to this appender.

Parameters
newAppenderappender to add, may not be null.

Implements log4cxx::spi::AppenderAttachable.

◆ append()

void log4cxx::AsyncAppender::append ( const spi::LoggingEventPtr & event,
helpers::Pool & p )
overridevirtual

Subclasses of AppenderSkeleton should implement this method to perform actual logging.

See also AppenderSkeleton::doAppend method.

Implements log4cxx::AppenderSkeleton.

◆ close()

void log4cxx::AsyncAppender::close ( )
overridevirtual

Close this AsyncAppender by interrupting the dispatcher thread which will process all pending events before exiting.

Implements log4cxx::Appender.

◆ doAppend()

void log4cxx::AsyncAppender::doAppend ( const spi::LoggingEventPtr & event,
helpers::Pool & pool )
overridevirtual

This method performs threshold checks and invokes filters before delegating actual logging to the subclasses specific AppenderSkeleton::append method.

Reimplemented from log4cxx::AppenderSkeleton.

◆ getAllAppenders()

AppenderList log4cxx::AsyncAppender::getAllAppenders ( ) const
overridevirtual

Get iterator over attached appenders.

Returns
list of all attached appenders.

Implements log4cxx::spi::AppenderAttachable.

◆ getAppender()

AppenderPtr log4cxx::AsyncAppender::getAppender ( const LogString & name) const
overridevirtual

Get appender by name.

Parameters
namename, may not be null.
Returns
matching appender or null.

Implements log4cxx::spi::AppenderAttachable.

◆ getBlocking()

bool log4cxx::AsyncAppender::getBlocking ( ) const

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()

int log4cxx::AsyncAppender::getBufferSize ( ) const

Gets the current buffer size.

Returns
the current value of the BufferSize option.

◆ getLocationInfo()

bool log4cxx::AsyncAppender::getLocationInfo ( ) const

The current value of the (unused) LocationInfo option.

◆ isAttached()

bool log4cxx::AsyncAppender::isAttached ( const AppenderPtr appender) const
overridevirtual

Determines if specified appender is attached.

Parameters
appenderappender.
Returns
true if attached.

Implements log4cxx::spi::AppenderAttachable.

◆ removeAllAppenders()

void log4cxx::AsyncAppender::removeAllAppenders ( )
overridevirtual

Removes and closes all attached appenders.

Implements log4cxx::spi::AppenderAttachable.

◆ removeAppender() [1/2]

void log4cxx::AsyncAppender::removeAppender ( const AppenderPtr appender)
overridevirtual

Removes an appender.

Parameters
appenderappender to remove.

Implements log4cxx::spi::AppenderAttachable.

◆ removeAppender() [2/2]

void log4cxx::AsyncAppender::removeAppender ( const LogString & name)
overridevirtual

Remove appender by name.

Parameters
namename.

Implements log4cxx::spi::AppenderAttachable.

◆ replaceAppender()

bool log4cxx::AsyncAppender::replaceAppender ( const AppenderPtr & oldAppender,
const AppenderPtr & newAppender )

Replace oldAppender with newAppender.

Returns
true if oldAppender was replaced with newAppender.

◆ replaceAppenders()

void log4cxx::AsyncAppender::replaceAppenders ( const AppenderList & newList)

Replace any previously added appenders with newList.

◆ requiresLayout()

bool log4cxx::AsyncAppender::requiresLayout ( ) const
overridevirtual

Configurators call this method to determine if the appender requires a layout.

If this method returns true, meaning that layout is required, then the configurator will configure an layout using the configuration information at its disposal. If this method returns false, 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.

Implements log4cxx::Appender.

◆ setBlocking()

void log4cxx::AsyncAppender::setBlocking ( bool value)

Sets whether appender should wait if there is no space available in the event buffer or immediately return.

Parameters
valuetrue if appender should wait until available space in buffer.

◆ setBufferSize()

void log4cxx::AsyncAppender::setBufferSize ( int size)

The BufferSize option takes a non-negative integer value.

This integer value determines the maximum size of the bounded buffer.

◆ setLocationInfo()

void log4cxx::AsyncAppender::setLocationInfo ( bool flag)

The LocationInfo attribute is provided for compatibility with log4j and has no effect on the log output.

Parameters
flagnew value.

◆ setOption()

void log4cxx::AsyncAppender::setOption ( const LogString & option,
const LogString & value )
overridevirtual

Set option to value.

Supported options Supported values Default value
BufferSize int 128
Blocking True,False True
See also
AppenderSkeleton::setOption()

Reimplemented from log4cxx::AppenderSkeleton.


The documentation for this class was generated from the following file: