Class: SemanticLogger::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/processor.rb

Overview

Thread that submits and processes log requests

Class Method Summary collapse

Class Method Details

.<<(log) ⇒ Object

Add log request to the queue for processing.



39
40
41
42
43
44
# File 'lib/semantic_logger/processor.rb', line 39

def self.<<(log)
  return unless active?

  call_log_subscribers(log)
  queue << log
end

.active?Boolean

Returns true if the appender_thread is active

Returns:

  • (Boolean)


13
14
15
# File 'lib/semantic_logger/processor.rb', line 13

def self.active?
  @thread && @thread.alive?
end

.closeObject

Close all appenders and flush any outstanding messages.



34
35
36
# File 'lib/semantic_logger/processor.rb', line 34

def self.close
  submit_request(:close)
end

.flushObject

Flush all queued log entries disk, database, etc.

All queued log messages are written and then each appender is flushed in turn.


29
30
31
# File 'lib/semantic_logger/processor.rb', line 29

def self.flush
  submit_request(:flush)
end

.lag_check_intervalObject

Returns the check_interval which is the number of messages between checks to determine if the appender thread is falling behind



74
75
76
# File 'lib/semantic_logger/processor.rb', line 74

def self.lag_check_interval
  @lag_check_interval
end

.lag_check_interval=(lag_check_interval) ⇒ Object

Set the check_interval which is the number of messages between checks to determine if the appender thread is falling behind



80
81
82
# File 'lib/semantic_logger/processor.rb', line 80

def self.lag_check_interval=(lag_check_interval)
  @lag_check_interval = lag_check_interval
end

.lag_threshold_sObject

Returns the amount of time in seconds to determine if the appender thread is falling behind



86
87
88
# File 'lib/semantic_logger/processor.rb', line 86

def self.lag_threshold_s
  @lag_threshold_s
end

.logger=(logger) ⇒ Object

Allow the internal logger to be overridden from its default of STDERR

Can be replaced with another Ruby logger or Rails logger, but never to
SemanticLogger::Logger itself since it is for reporting problems
while trying to log to the various appenders


68
69
70
# File 'lib/semantic_logger/processor.rb', line 68

def self.logger=(logger)
  @logger = logger
end

.on_log(object = nil, &block) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/semantic_logger/processor.rb', line 103

def self.on_log(object = nil, &block)
  subscriber = block || object

  raise('When supplying an on_log subscriber, it must support the #call method') unless subscriber.is_a?(Proc) || subscriber.respond_to?(:call)
  subscribers = (@log_subscribers ||= Concurrent::Array.new)
  subscribers << subscriber unless subscribers.include?(subscriber)
end

.on_metric(options = {}, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/semantic_logger/processor.rb', line 90

def self.on_metric(options = {}, &block)
  # Backward compatibility
  options    = options.is_a?(Hash) ? options.dup : {appender: options}
  subscriber = block || options.delete(:appender)

  # Convert symbolized metrics appender to an actual object
  subscriber = SemanticLogger::Appender.constantize_symbol(subscriber, 'SemanticLogger::Metrics').new(options) if subscriber.is_a?(Symbol)

  raise('When supplying a metrics subscriber, it must support the #call method') unless subscriber.is_a?(Proc) || subscriber.respond_to?(:call)
  subscribers = (@metric_subscribers ||= Concurrent::Array.new)
  subscribers << subscriber unless subscribers.include?(subscriber)
end

.queue_sizeObject

Returns [Integer] the number of log entries waiting to be written to the appenders.

When this number grows it is because the logging appender thread is not able to write to the appenders fast enough. Either reduce the amount of logging, increase the log level, reduce the number of appenders, or look into speeding up the appenders themselves



23
24
25
# File 'lib/semantic_logger/processor.rb', line 23

def self.queue_size
  queue.size
end

.startObject

Start the appender thread



5
6
7
8
9
10
# File 'lib/semantic_logger/processor.rb', line 5

def self.start
  return false if active?
  @thread = Thread.new { process_requests }
  raise 'Failed to start Appender Thread' unless @thread
  true
end

.submit_request(command) ⇒ Object

Submit command and wait for reply



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/semantic_logger/processor.rb', line 47

def self.submit_request(command)
  return false unless active?

  msg = "Too many queued log messages: #{queue_size}, running command: #{command}"
  if queue_size > 1_000
    logger.warn msg
  elsif queue_size > 100
    logger.info msg
  elsif queue_size > 0
    logger.trace msg
  end

  reply_queue = Queue.new
  queue << {command: command, reply_queue: reply_queue}
  reply_queue.pop
end