Class: SemanticLogger::Logger

Inherits:
Base
  • Object
show all
Includes:
Concerns::Compatibility
Defined in:
lib/semantic_logger/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

Direct Known Subclasses

DebugAsTraceLogger

Class Attribute Summary collapse

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

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #payload, #pop_tags, #push_tags, #should_log?, #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 = SemanticLogger::Logger.new(self)

OR

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

Parameters:

klass
  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

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


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

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

Class Attribute Details

.subscribersObject (readonly)

Returns the value of attribute subscribers.



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

def subscribers
  @subscribers
end

Class Method Details

.processorObject



23
24
25
# File 'lib/semantic_logger/logger.rb', line 23

def self.processor
  @processor
end

.subscribe(object = nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/semantic_logger/logger.rb', line 8

def self.subscribe(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 = (@subscribers ||= Concurrent::Array.new)
  subscribers << subscriber unless subscribers.include?(subscriber)
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

Subscribers are called inline before handing off to the queue so that they can capture additional context information as needed.



57
58
59
60
61
62
63
64
# File 'lib/semantic_logger/logger.rb', line 57

def log(log, message = nil, progname = nil, &block)
  # Compatibility with ::Logger
  return add(log, message, progname, &block) unless log.is_a?(SemanticLogger::Log)

  Logger.call_subscribers(log)

  Logger.processor.log(log)
end