public abstract class AbstractAppender<WriterConfigType,AppenderStatsType extends AbstractAppenderStatistics,AppenderStatsMXBeanType>
extends org.apache.log4j.AppenderSkeleton
For the most part, appenders have the same behavior: they initialize, transform log messages, and shut down. Most of the code to do that lives here, with a few hooks that are implemented in the appender proper.
Note that some behaviors, such as log rotation, are implemented here even if they
are not supported by all appenders. The appenders that do not support those
behaviors are responsible for disabling them. For example, an appender that does
not support log rotation should throw if setRotationMode(java.lang.String) is called.
Note that most of the member variables defined by this class are protected. This is primarily to support testing, but also follows my philosophy of related classes: there is no logical difference between direct access properties; in both cases you're defining an API. Of course, this is a public API for an internal class, so any application code that touches these variables should not be surprised if they cease to exist.
| Modifier and Type | Field and Description |
|---|---|
protected AppenderStatsType |
appenderStats |
protected long |
batchDelay |
protected String |
clientEndpoint |
protected String |
clientFactory |
protected MessageQueue.DiscardAction |
discardAction |
protected int |
discardThreshold |
protected int |
lastRotationCount |
protected long |
lastRotationTimestamp |
protected long |
rotationInterval |
protected RotationMode |
rotationMode |
protected AtomicInteger |
sequence |
protected ThreadFactory |
threadFactory |
protected LogWriter |
writer |
protected WriterFactory<WriterConfigType,AppenderStatsType> |
writerFactory |
| Constructor and Description |
|---|
AbstractAppender(ThreadFactory threadFactory,
WriterFactory<WriterConfigType,AppenderStatsType> writerFactory,
AppenderStatsType appenderStats,
Class<AppenderStatsMXBeanType> appenderStatsMXBeanClass) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
append(org.apache.log4j.spi.LoggingEvent event) |
void |
close() |
protected abstract WriterConfigType |
generateWriterConfig()
Called just before a writer is created, so that the subclass can
perform substitutions on the configuration.
|
AppenderStatsType |
getAppenderStatistics()
Returns the appender statistics object.
|
long |
getBatchDelay()
Returns the maximum batch delay; see
setBatchDelay(long). |
String |
getClientEndpoint()
Returns the current service endpoint.
|
String |
getClientFactory()
Returns the current AWS client factory class/method name.
|
String |
getDiscardAction()
Returns the configured discard action.
|
int |
getDiscardThreshold()
Returns the configured discard threshold.
|
long |
getRotationInterval()
Returns the current rotation interval.
|
String |
getRotationMode()
Returns the current rotation mode.
|
int |
getSequence()
Returns the current log sequence number.
|
protected abstract boolean |
isMessageTooLarge(LogMessage message)
Called
append(org.apache.log4j.spi.LoggingEvent) to ensure that we don't have a single message
that violates AWS batching rules. |
protected void |
registerStatisticsBean()
Registers the appender statistics with JMX.
|
boolean |
requiresLayout() |
protected void |
rotate()
Rotates the log writer.
|
void |
setBatchDelay(long value)
Sets the maximum batch delay, in milliseconds.
|
void |
setClientEndpoint(String value)
Sets the service endpoint.
|
void |
setClientFactory(String value)
Sets a static AWS client factory method, which will be called instead of
the writer's internal client factory.
|
void |
setDiscardAction(String value)
Sets the action to take when the number of unsent messages exceeds the discard
threshold.
|
void |
setDiscardThreshold(int value)
Sets the number of unsent messages that will trigger message discard.
|
void |
setRotationInterval(long value)
Sets the rotation interval, for those appenders that support rotation.
|
void |
setRotationMode(String value)
Sets the rule for log stream rotation, for those appenders that support rotation.
|
void |
setSequence(int value)
Sets the log sequence number, used by the
{sequence} substitution variable. |
protected void |
unregisterStatisticsBean()
Unregisters the appender statistics from JMX.
|
protected ThreadFactory threadFactory
protected WriterFactory<WriterConfigType,AppenderStatsType extends AbstractAppenderStatistics> writerFactory
protected AppenderStatsType extends AbstractAppenderStatistics appenderStats
protected volatile LogWriter writer
protected volatile long lastRotationTimestamp
protected volatile int lastRotationCount
protected long batchDelay
protected int discardThreshold
protected MessageQueue.DiscardAction discardAction
protected RotationMode rotationMode
protected long rotationInterval
protected AtomicInteger sequence
protected String clientFactory
protected String clientEndpoint
public AbstractAppender(ThreadFactory threadFactory, WriterFactory<WriterConfigType,AppenderStatsType> writerFactory, AppenderStatsType appenderStats, Class<AppenderStatsMXBeanType> appenderStatsMXBeanClass)
public void setBatchDelay(long value)
The writer attempts to gather multiple logging messages into a batch, to reduce communication with the service. The batch delay controls the time that a message will remain in-memory while the writer builds this batch. In a low-volume environment it will be the main determinant of when the batch is sent; in a high volume environment it's likely that the maximum request size will be reached before the batch delay expires.
The default value is 2000, which is rather arbitrarily chosen.
public long getBatchDelay()
setBatchDelay(long). Primarily used
for testing.public void setDiscardThreshold(int value)
The default, 10,000, is based on the assumptions that (1) each message will be 1k or less, and (2) any app that uses remote logging can afford 10MB.
Note: at present, discard threshold cannot be changed after creating a log writer. For appenders that rotate logs, a new configuration value will be recognized at the time of rotation.
public int getDiscardThreshold()
public void setDiscardAction(String value)
The default is "oldest". Attempting to set an incorrect value will throw a configuration error.
Note: at present, discard action cannot be changed after creating a log writer. For appenders that rotate logs, a new configuration value will be recognized at the time of rotation.
public String getDiscardAction()
public void setRotationMode(String value)
RotationMode for values.
Attempting to set an invalid mode is equivalent to "none", but will emit a warning to the Log4J internal log.
public String getRotationMode()
public void setRotationInterval(long value)
rotationMode parameter is "interval" or "count":
for the former, it's the number of milliseconds between rotations, for the latter the
number of messages.
If using interval rotation, you should include {timestamp} in the log stream
name. If using counted rotation, you should include {sequence}.
public long getRotationInterval()
public void setSequence(int value)
{sequence} substitution variable.public int getSequence()
public void setClientFactory(String value)
The passed string is of the form com.example.Classname.methodName.
If this does not reference a class/method on the classpath then writer
initialization will fail.
Calling this method after the writer has been initialized will have no effect (except for those appenders that rotate logs, in which case it will apply to the post-rotate writer).
public String getClientFactory()
public void setClientEndpoint(String value)
public String getClientEndpoint()
public AppenderStatsType getAppenderStatistics()
protected void append(org.apache.log4j.spi.LoggingEvent event)
append in class org.apache.log4j.AppenderSkeletonpublic void close()
public boolean requiresLayout()
protected void rotate()
protected abstract WriterConfigType generateWriterConfig()
protected abstract boolean isMessageTooLarge(LogMessage message)
append(org.apache.log4j.spi.LoggingEvent) to ensure that we don't have a single message
that violates AWS batching rules.protected void registerStatisticsBean()
The name for the bean is consistent with the Log4J LayoutDynamicMBean,
so that it will appear in the hierarchy under the appender.
Note: this method is protected so that it can be avoided during unit tests.
protected void unregisterStatisticsBean()
Note: this method is protected so that it can be avoided during unit tests.
Copyright © 2018. All rights reserved.