Class: SemanticLogger::Processor

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

Overview

Thread that submits and processes log requests

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



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

def initialize
  @log_subscribers = nil
  @logger          = self.class.logger.dup
  @logger.name     = self.class.name
end

Class Attribute Details

.loggerObject

Internal logger for SemanticLogger

For example when an appender is not working etc..
By default logs to STDERR


70
71
72
73
74
75
76
# File 'lib/semantic_logger/processor.rb', line 70

def self.logger
  @logger ||= begin
    l      = SemanticLogger::Appender::File.new(io: STDERR, level: :warn)
    l.name = name
    l
  end
end

Instance Attribute Details

#log_subscribersObject

Returns the value of attribute log_subscribers.



78
79
80
# File 'lib/semantic_logger/processor.rb', line 78

def log_subscribers
  @log_subscribers
end

#loggerObject

Returns the value of attribute logger.



78
79
80
# File 'lib/semantic_logger/processor.rb', line 78

def logger
  @logger
end

Class Method Details

.<<(log) ⇒ Object

Add log request to the queue for processing. Log subscribers are called inline before handing off to the queue.



36
37
38
39
# File 'lib/semantic_logger/processor.rb', line 36

def self.<<(log)
  instance.appender.send(:call_log_subscribers, log)
  instance.log(log)
end

.active?Boolean

Returns true if the appender_thread is active

Returns:

  • (Boolean)


20
21
22
# File 'lib/semantic_logger/processor.rb', line 20

def self.active?
  instance.alive?
end

.instanceObject

Returns [Appender::Async => SemanticLogger::Processor] the global instance of this processor wrapped in the Async proxy so that all logging is asynchronous in a thread of its own.

More than one instance can be created if needed.



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

def self.instance
  @processor
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.



43
44
45
# File 'lib/semantic_logger/processor.rb', line 43

def self.lag_check_interval
  instance.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.



49
50
51
# File 'lib/semantic_logger/processor.rb', line 49

def self.lag_check_interval=(lag_check_interval)
  instance.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.



55
56
57
# File 'lib/semantic_logger/processor.rb', line 55

def self.lag_threshold_s
  instance.lag_threshold_s
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



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

def self.queue_size
  instance.queue.size
end

.startObject

Start the appender thread



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

def self.start
  return false if instance.active?
  instance.thread
  true
end

Instance Method Details

#closeObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/semantic_logger/processor.rb', line 119

def close
  SemanticLogger.appenders.each do |appender|
    begin
      logger.trace "Appender thread: Closing appender: #{appender.name}"
      appender.flush
      appender.close
      SemanticLogger.remove_appender(appender)
    rescue Exception => exc
      logger.error "Appender thread: Failed to close appender: #{appender.inspect}", exc
    end
  end
  logger.trace 'Appender thread: All appenders closed and removed from appender list'
end

#flushObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/semantic_logger/processor.rb', line 107

def flush
  SemanticLogger.appenders.each do |appender|
    begin
      logger.trace "Appender thread: Flushing appender: #{appender.name}"
      appender.flush
    rescue Exception => exc
      logger.error "Appender thread: Failed to flush appender: #{appender.inspect}", exc
    end
  end
  logger.trace 'Appender thread: All appenders flushed'
end

#log(log) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/semantic_logger/processor.rb', line 97

def log(log)
  SemanticLogger.appenders.each do |appender|
    begin
      appender.log(log) if appender.should_log?(log)
    rescue Exception => exc
      logger.error "Appender thread: Failed to log to appender: #{appender.inspect}", exc
    end
  end
end

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



86
87
88
89
90
91
92
93
94
95
# File 'lib/semantic_logger/processor.rb', line 86

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

  unless subscriber.is_a?(Proc) || subscriber.respond_to?(:call)
    raise('When supplying an on_log subscriber, it must support the #call method')
  end

  subscribers = (@log_subscribers ||= Concurrent::Array.new)
  subscribers << subscriber unless subscribers.include?(subscriber)
end