Class: SemanticLogger::Appender::AsyncBatch
- 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
-
#batch_seconds ⇒ Object
Returns the value of attribute batch_seconds.
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#signal ⇒ Object
readonly
Returns the value of attribute signal.
Attributes inherited from Async
#appender, #lag_check_interval, #lag_threshold_s, #max_queue_size, #queue
Instance Method Summary collapse
-
#initialize(appender:, max_queue_size: 10_000, lag_threshold_s: 30, batch_size: 300, batch_seconds: 5) ⇒ AsyncBatch
constructor
Batching Appender proxy for appenders that support batches.
-
#log(log) ⇒ Object
Add log message for processing.
Methods inherited from Async
#active?, #capped?, #close, #flush, #reopen, #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 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.
28 29 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 28 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 ) return if appender.respond_to?(:batch) raise(ArgumentError, "#{appender.class.name} does not support batching. It must implement #batch") end |
Instance Attribute Details
#batch_seconds ⇒ Object
Returns the value of attribute batch_seconds.
9 10 11 |
# File 'lib/semantic_logger/appender/async_batch.rb', line 9 def batch_seconds @batch_seconds end |
#batch_size ⇒ Object
Returns the value of attribute batch_size.
9 10 11 |
# File 'lib/semantic_logger/appender/async_batch.rb', line 9 def batch_size @batch_size end |
#signal ⇒ Object (readonly)
Returns the value of attribute signal.
10 11 12 |
# File 'lib/semantic_logger/appender/async_batch.rb', line 10 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 |