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
@@appender_thread =
nil

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

.appender_threadObject

Separate appender thread responsible for reading log messages and calling the appenders in it’s thread rubocop:disable BlockNesting, AssignmentInCondition, PerceivedComplexity, CyclomaticComplexity, AbcSize, LineLength, RescueException



151
152
153
# File 'lib/sapience/logger.rb', line 151

def self.appender_thread
  @@appender_thread
end

.appender_thread_active?Boolean

Returns true if the appender_thread is active

Returns:

  • (Boolean)


144
145
146
# File 'lib/sapience/logger.rb', line 144

def self.appender_thread_active?
  @@appender_thread && @@appender_thread.running?
end

.closeObject

Close all appenders and flush any outstanding messages



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sapience/logger.rb', line 54

def self.close
  return unless appender_thread
  appender_thread << lambda do
    Sapience.appenders.each do |appender|
      begin
        close_appender(appender)
      rescue StandardError => exc
        logger.error "Appender thread: Failed to close appender: #{appender.inspect}", exc
      end
    end

    logger.trace "Appender thread: All appenders flushed"
  end
end

.close_appender(appender) ⇒ Object



69
70
71
72
73
74
# File 'lib/sapience/logger.rb', line 69

def self.close_appender(appender)
  logger.trace "Appender thread: Closing appender: #{appender.name}"
  appender.flush
  appender.close
  Sapience.remove_appender(appender)
end

.flushObject

Flush all queued log entries disk, database, etc.

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sapience/logger.rb', line 37

def self.flush # rubocop:disable AbcSize
  return unless appender_thread
  appender_thread << lambda do
    Sapience.appenders.each do |appender|
      begin
        logger.trace "Appender thread: Flushing appender: #{appender.name}"
        appender.flush
      rescue StandardError => exc
        logger.error "Appender thread: Failed to flush appender: #{appender.inspect}", exc
      end
    end

    logger.trace "Appender thread: All appenders flushed"
  end
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



81
82
83
# File 'lib/sapience/logger.rb', line 81

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



87
88
89
# File 'lib/sapience/logger.rb', line 87

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



93
94
95
# File 'lib/sapience/logger.rb', line 93

def self.lag_threshold_s
  @@lag_threshold_s
end

.loggerObject

Internal logger for Sapience

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


126
127
128
129
130
131
132
# File 'lib/sapience/logger.rb', line 126

def self.logger
  @@logger ||= begin
    l      = Sapience::Appender::Stream.new(io: STDERR, level: :warn)
    l.name = name
    l
  end
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


101
102
103
# File 'lib/sapience/logger.rb', line 101

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

.start_appender_threadObject

Start the appender thread



135
136
137
138
139
140
141
# File 'lib/sapience/logger.rb', line 135

def self.start_appender_thread
  return false if appender_thread_active?

  @@appender_thread = Concurrent::SingleThreadExecutor.new
  fail "Failed to start Appender Thread" unless @@appender_thread
  true
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



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sapience/logger.rb', line 107

def log(log, message = nil, progname = nil, &block)
  # Compatibility with ::Logger
  return add(log, message, progname, &block) unless log.is_a?(Sapience::Log)
  @@appender_thread.post(log) do |log_message|
    Sapience.appenders.each do |appender|
      begin
        appender.log(log_message)
      rescue StandardError => exc
        logger.error "Appender thread: Failed to log to appender: #{appender.inspect}", exc
      end
    end
  end if @@appender_thread
end