Class: SemanticLogger::Appender::AsyncBatch

Inherits:
Async
  • Object
show all
Defined in:
lib/semantic_logger/appender/async_batch.rb

Overview

Log asynchronously in batches using a separate thread.

Log messages are grouped up and only logged when:

  • The number of queued messages is exceeded.

  • Or, the appropriate amount of time has passed since the last batch was sent.

Instance Attribute Summary collapse

Attributes inherited from Async

#appender, #lag_check_interval, #lag_threshold_s, #logger, #queue

Instance Method Summary collapse

Methods inherited from Async

#active?, #capped?, #close, #flush, #thread

Constructor Details

#initialize(appender:, max_queue_size: 10_000, lag_threshold_s: 30, batch_size: 300, batch_seconds: 5) ⇒ AsyncBatch

Batching Appender proxy for appenders that support batches.

Parameters:

batch_size: [Integer]
  Maximum number of messages to batch up before sending.
  Default: 300

batch_seconds: [Integer]
  Maximum number of seconds between sending batches.
  Default: 5

See SemanticLogger::Appender::Async for other paramaters

Note:

  • ‘lag_check_interval` is not applicable to batches, since the first message of every batch is the oldest and is always checked to see if the lag interval has been exceeded.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/semantic_logger/appender/async_batch.rb', line 30

def initialize(appender:,
               max_queue_size: 10_000,
               lag_threshold_s: 30,
               batch_size: 300,
               batch_seconds: 5)

  @batch_size    = batch_size
  @batch_seconds = batch_seconds
  @signal        = Concurrent::Event.new
  super(
    appender:        appender,
    max_queue_size:  max_queue_size,
    lag_threshold_s: lag_threshold_s
  )

  raise(ArgumentError, "#{appender.class.name} does not support batching. It must implement #batch") unless appender.respond_to?(:batch)
end

Instance Attribute Details

#batch_secondsObject

Returns the value of attribute batch_seconds.



11
12
13
# File 'lib/semantic_logger/appender/async_batch.rb', line 11

def batch_seconds
  @batch_seconds
end

#batch_sizeObject

Returns the value of attribute batch_size.



11
12
13
# File 'lib/semantic_logger/appender/async_batch.rb', line 11

def batch_size
  @batch_size
end

#signalObject (readonly)

Returns the value of attribute signal.



12
13
14
# File 'lib/semantic_logger/appender/async_batch.rb', line 12

def signal
  @signal
end

Instance Method Details

#log(log) ⇒ Object

Add log message for processing.



49
50
51
52
53
54
# File 'lib/semantic_logger/appender/async_batch.rb', line 49

def log(log)
  result = super(log)
  # Wake up the processing thread since the number of queued messages has been exceeded.
  signal.set if queue.size >= batch_size
  result
end