Class: SemanticLogger::Logger

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

Constant Summary collapse

@@lag_check_interval =
5000
@@lag_threshold_s =
30

Instance Attribute Summary collapse

Attributes inherited from Base

#level, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

default_level, default_level=, #payload, #pop_tags, #push_tags, #tagged, #tags, #with_payload

Constructor Details

#initialize(klass, level = nil) ⇒ Logger

Returns a Logger instance

Return the logger for a specific class, supports class specific log levels

logger = SemanticLogger::Logger.new(self)

OR

logger = SemanticLogger::Logger.new('MyClass')

Parameters:

application
  A class, module or a string with the application/class name
  to be used in the logger

level
  The initial log level to start with for this logger instance
  Default: SemanticLogger.default_level


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

def initialize(klass, level=nil)
  @name = klass.is_a?(String) ? klass : klass.name
  self.level = level || SemanticLogger.default_level
end

Instance Attribute Details

#formatterObject

DO NOT USE. Adding unused formatter to support Rails 4 logging Formatters must be set at the appender level, not at the logger level

Due to the following code in Rails::Server#start that cannot be changed without patching the entire method

console = ActiveSupport::Logger.new($stdout)
console.formatter = Rails.logger.formatter
console.level = Rails.logger.level

Rails.logger.extend(ActiveSupport::Logger.broadcast(console))


18
19
20
# File 'lib/semantic_logger/logger.rb', line 18

def formatter
  @formatter
end

Class Method Details

.appendersObject

DEPRECATED See SemanticLogger.add_appender



97
98
99
100
# File 'lib/semantic_logger/logger.rb', line 97

def self.appenders
  warn "[DEPRECATION] `SemanticLogger::Logger.appenders` is deprecated.  Please use `SemanticLogger.add_appender` instead."
  SemanticLogger.appenders
end

.cache_countObject

DEPRECATED: Please use queue_size instead.



103
104
105
106
# File 'lib/semantic_logger/logger.rb', line 103

def self.cache_count
  warn "[DEPRECATION] 'SemanticLogger::Logger.cache_count' is deprecated.  Please use 'SemanticLogger::Logger.queue_size' instead."
  queue_size
end

.flushObject

Flush all queued log entries disk, database, etc.

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


54
55
56
57
58
59
60
61
# File 'lib/semantic_logger/logger.rb', line 54

def self.flush
  return false unless appender_thread_active?

  logger.debug "Flushing appenders with #{queue_size} log messages on the queue"
  reply_queue = Queue.new
  queue << { :command => :flush, :reply_queue => reply_queue }
  reply_queue.pop
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



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

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



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

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



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

def self.lag_threshold_s
  @@lag_threshold_s
end

.logger=(logger) ⇒ Object

Allow the internal logger to be overridden from its default to 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


92
93
94
# File 'lib/semantic_logger/logger.rb', line 92

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

.on_metric(&block) ⇒ Object

Supply a block to be called whenever a metric is seen during benchmark logging

Parameters
  block
    The block to be called

Example:

SemanticLogger.on_metric do |log_struct|
  puts "#{log_struct.metric} was received. Log Struct: #{log_struct.inspect}"
end


118
119
120
# File 'lib/semantic_logger/logger.rb', line 118

def self.on_metric(&block)
  (@@metric_subscribers  ||= ThreadSafe::Array.new) << block
end

.queue_sizeObject

Returns [Integer] the number of log entries that have not been 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



48
49
50
# File 'lib/semantic_logger/logger.rb', line 48

def self.queue_size
  queue.size
end

.time_threshold_s=(time_threshold_s) ⇒ Object



84
85
86
# File 'lib/semantic_logger/logger.rb', line 84

def self.time_threshold_s=(time_threshold_s)
  @@lag_threshold_s = time_threshold_s
end