Package org.logevents.observers
Class AbstractBatchingLogEventObserver
- java.lang.Object
-
- org.logevents.core.AbstractFilteredLogEventObserver
-
- org.logevents.observers.AbstractBatchingLogEventObserver
-
- All Implemented Interfaces:
LogEventObserver
- Direct Known Subclasses:
AbstractHttpPostJsonLogEventObserver
,DatabaseLogEventObserver
,ElasticsearchLogEventObserver
,SmtpLogEventObserver
,SystemTrayLogEventObserver
,TcpLogEventObserver
public abstract class AbstractBatchingLogEventObserver extends AbstractFilteredLogEventObserver
Used to gather up a number of log event to process as a batch. This is useful when using logging destinations where high frequency of messages would be inefficient or noisy, such as email or Slack and for logging asynchronously to external systems over network such as databases or Elasticsearch.AbstractBatchingLogEventObserver
can decide how to batch events with batchers, for exampleThrottlingBatcher
andCooldownBatcher
. When processing, theAbstractBatchingLogEventObserver
callsprocessBatch(List)
with the whole batch to process the log events, which should be overridden by subclasses. Consecutive Log events with the same message pattern and log level are grouped together so the processor can easily ignore duplicate messages.Configuration example with cooldown (don't send messages too frequently)
The following will send the send messages at level WARN that don't have theMarker
"PERSONAL". After each batch, wait at least 10 seconds before sending the next batch. But after each message is received, wait 2 seconds to see if more messages are coming. In any case, never wait more than 30 seconds before sending a batch.observer.sample.threshold=WARN observer.sample.suppressMarkers=PERSONAL_DATA observer.sample.idleThreshold=PT2S observer.sample.cooldownTime=PT10S observer.sample.maximumWaitTime=PT30S
Configuration example with throttle (increasingly larger offsets)
The following will send the first message at level WARN with theMarker
"DAEMON" immediately, then wait at least 30 seconds before sending the next, then 5 minutes, then 15 minutes between each batch.observer.sample.threshold=WARN observer.sample.requireMarker=DAEMON observer.sample.throttle=PT30S PT5M PT15M
Marker-specific configuration
The following will throttle messages with MY_MARKER, grouped by MDC value userId. If there is a log event with an unused userId, it will be sent immediately. Then all events for the next minute for the same userId will be collected, then the next hour and then the next day. If there are log events for other userIds, they will be batched separately. This is useful for situations like API batch users where the credentials have expired, for example.observer.sample.markers.MY_MARKER.throttle=PT1M PT1H PT24H observer.sample.markers.MY_MARKER.mdc=userId
- Author:
- Johannes Brodwall
- See Also:
SlackLogEventObserver
,MicrosoftTeamsLogEventObserver
,SmtpLogEventObserver
,ElasticsearchLogEventObserver
,DatabaseLogEventObserver
-
-
Field Summary
Fields Modifier and Type Field Description protected LogEventObserver
defaultBatcher
protected ScheduledExecutorService
executor
protected static ScheduledExecutorService
scheduledExecutorService
protected static LogEventShutdownHook
shutdownHook
-
Constructor Summary
Constructors Constructor Description AbstractBatchingLogEventObserver()
AbstractBatchingLogEventObserver(ScheduledExecutorService executor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
configureBatching(Configuration configuration)
ReadidleThreshold
,cooldownTime
andmaximumWaitTime
from configurationvoid
configureMarkers(Configuration configuration)
Configure throttling for the provided marker.protected LogEventObserver
createBatcher(Configuration configuration, String markerName)
protected LogEventObserver
createMdcBatcher(BatcherFactory batcherFactory, Configuration configuration, String markerName, String mdcKey)
protected Consumer<List<LogEvent>>
createProcessor(Configuration configuration, String markerName)
protected void
doLogEvent(LogEvent logEvent)
protected LogEventObserver
getBatcher(LogEvent logEvent)
protected BatcherFactory
getBatcherFactory(Configuration configuration, String prefix)
protected LogEventObserver
getDefaultBatcher()
protected LogEventObserver
getMarkerBatcher(org.slf4j.Marker myMarker)
protected void
processBatch(List<LogEvent> batch)
protected abstract void
processBatch(LogEventBatch batch)
String
toString()
-
Methods inherited from class org.logevents.core.AbstractFilteredLogEventObserver
configureFilter, filteredOn, getCondition, getThreshold, isEnabled, logEvent, setCondition, setFilter, setThreshold, shouldLogEvent
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.logevents.LogEventObserver
isEnabled, stream, toList
-
-
-
-
Field Detail
-
scheduledExecutorService
protected static ScheduledExecutorService scheduledExecutorService
-
shutdownHook
protected static LogEventShutdownHook shutdownHook
-
defaultBatcher
protected LogEventObserver defaultBatcher
-
executor
protected ScheduledExecutorService executor
-
-
Constructor Detail
-
AbstractBatchingLogEventObserver
public AbstractBatchingLogEventObserver()
-
AbstractBatchingLogEventObserver
public AbstractBatchingLogEventObserver(ScheduledExecutorService executor)
-
-
Method Detail
-
configureBatching
protected void configureBatching(Configuration configuration)
ReadidleThreshold
,cooldownTime
andmaximumWaitTime
from configuration
-
doLogEvent
protected final void doLogEvent(LogEvent logEvent)
- Specified by:
doLogEvent
in classAbstractFilteredLogEventObserver
-
getBatcher
protected LogEventObserver getBatcher(LogEvent logEvent)
-
processBatch
protected abstract void processBatch(LogEventBatch batch)
-
configureMarkers
public void configureMarkers(Configuration configuration)
Configure throttling for the provided marker. Throttling is a string likePT10M PT30M
indicating a list of periods (PT). In this example, after one message is sent, all messages with this marker will be batched up for the next ten minutes (PT10M). If any messages were collected, the next batch will be sent after 30 minutes (PT30M). The last throttling period will repeat until a period passes with no batched messages.
-
createBatcher
protected LogEventObserver createBatcher(Configuration configuration, String markerName)
-
createProcessor
protected Consumer<List<LogEvent>> createProcessor(Configuration configuration, String markerName)
-
getBatcherFactory
protected BatcherFactory getBatcherFactory(Configuration configuration, String prefix)
-
createMdcBatcher
protected LogEventObserver createMdcBatcher(BatcherFactory batcherFactory, Configuration configuration, String markerName, String mdcKey)
-
getMarkerBatcher
protected LogEventObserver getMarkerBatcher(org.slf4j.Marker myMarker)
-
getDefaultBatcher
protected LogEventObserver getDefaultBatcher()
-
-