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

@@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

#application, #default_formatter, #fast_tag, #host, #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


114
115
116
# File 'lib/sapience/logger.rb', line 114

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



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

def self.appender_thread
  @@appender_thread
end

.appender_thread_active?Boolean

Returns true if the appender_thread is active

Returns:

  • (Boolean)


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

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

.closeObject

Close all appenders and flush any outstanding messages



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sapience/logger.rb', line 29

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



44
45
46
47
48
49
# File 'lib/sapience/logger.rb', line 44

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


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sapience/logger.rb', line 12

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.class.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

.loggerObject

Internal logger for Sapience

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


56
57
58
59
60
61
62
# File 'lib/sapience/logger.rb', line 56

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


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

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

.start_appender_threadObject

Start the appender thread



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

def self.start_appender_thread
  return false if appender_thread_active?

  @@appender_thread = Sapience.log_executor_class.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



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sapience/logger.rb', line 120

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 << lambda do
    Sapience.appenders.each do |appender|
      begin
        appender.log(log)
      rescue StandardError => exc
        self.class.logger.error "Appender thread: Failed to log to appender: #{appender.inspect}", exc
      end
    end
  end if @@appender_thread
end