Class: Sapience::Logger

Inherits:
Base
  • Object
show all
Includes:
Concerns::Compatibility
Defined in:
lib/sapience/logger.rb

Overview

Logger stores the class name to be used for all log messages so that every log message written by this instance will include the class name

Constant Summary collapse

@@lag_check_interval =
5000
@@lag_threshold_s =
30

Instance Attribute Summary

Attributes inherited from Base

#filter, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Compatibility

#add, #close, included, #reopen

Methods inherited from Base

#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(klass, level = nil, filter = nil) ⇒ Logger

Returns a Logger instance

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

logger = Sapience::Logger.new(self)

OR

logger = Sapience::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: Sapience.config.default_level

filter [Regexp|Proc]
  RegExp: Only include log messages where the class name matches the supplied
  regular expression. All other messages will be ignored
  Proc: Only include log messages where the supplied Proc returns true
        The Proc must return true or false


31
32
33
# File 'lib/sapience/logger.rb', line 31

def initialize(klass, level = nil, filter = nil)
  super
end

Class Method Details

.closeObject

Close all appenders and flush any outstanding messages



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sapience/logger.rb', line 61

def self.close
  msg = "Closing appenders with #{queue_size} log messages on the queue"
  if queue_size > 1_000
    logger.warn msg
  elsif queue_size > 100
    logger.info msg
  elsif queue_size > 0
    logger.trace msg
  end
  process_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


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sapience/logger.rb', line 48

def self.flush
  msg = "Flushing appenders with #{queue_size} log messages on the queue"
  if queue_size > 1_000
    logger.warn msg
  elsif queue_size > 100
    logger.info msg
  elsif queue_size > 0
    logger.trace msg
  end
  process_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



78
79
80
# File 'lib/sapience/logger.rb', line 78

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



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

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



90
91
92
# File 'lib/sapience/logger.rb', line 90

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
Sapience::Logger itself since it is for reporting problems
while trying to log to the various appenders


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

def self.logger=(logger)
  @@logger = logger
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



42
43
44
# File 'lib/sapience/logger.rb', line 42

def self.queue_size
  queue.size
end

Instance Method Details

#log(log, message = nil, progname = nil, &block) ⇒ Object

Place log request on the queue for the Appender thread to write to each appender in the order that they were registered



104
105
106
107
108
# File 'lib/sapience/logger.rb', line 104

def log(log, message = nil, progname = nil, &block)
  # Compatibility with ::Logger
  return add(log, message, progname, &block) unless log.is_a?(Sapience::Log)
  self.class.queue << log if @@appender_thread
end