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, #instance_named_tags, #instance_tags, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Compatibility

#add, #close, included, #reopen

Methods inherited from Base

#backtrace, #benchmark, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags, #with_level

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



82
83
84
# File 'lib/semantic_logger/logger.rb', line 82

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

Class Attribute Details

.subscribers ⇒ Object (readonly)

Returns the value of attribute subscribers.



26
27
28
# File 'lib/semantic_logger/logger.rb', line 26

def subscribers
  @subscribers
end

Class Method Details

.call_subscribers(log) ⇒ Object

Calls the on_log subscribers, or in a Ractor other than the main one, the shareable ones.



105
106
107
108
109
110
111
112
113
114
# File 'lib/semantic_logger/logger.rb', line 105

def self.call_subscribers(log)
  subscribers = RactorSupport.other_ractor? ? @ractor_subscribers : @subscribers
  return unless subscribers

  subscribers.each do |subscriber|
    subscriber.call(log)
  rescue Exception => e
    processor.logger.error("Exception calling :on_log subscriber", e)
  end
end

.clear_subscribers ⇒ Object

Removes every on_log subscriber.



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

def self.clear_subscribers
  @subscribers&.clear
  @ractor_subscribers = nil
end

.processor ⇒ Object

Returns the processor that writes log entries to the appenders, or in a Ractor other than the main one, the RactorProcessor that sends them to the main Ractor.



43
44
45
46
47
# File 'lib/semantic_logger/logger.rb', line 43

def self.processor
  return RactorProcessor.current if RactorSupport.other_ractor?

  @processor ||= Processor.new
end

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/semantic_logger/logger.rb', line 7

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)
  return if subscribers.include?(subscriber)

  subscribers << subscriber
  # A Ractor other than the main one can only call a shareable subscriber.
  if RactorSupport.enabled? && ::Ractor.shareable?(subscriber)
    @ractor_subscribers = [*@ractor_subscribers, subscriber].freeze
  end
  subscribers
end

.sync! ⇒ Object

Switch to the synchronous processor



50
51
52
53
54
# File 'lib/semantic_logger/logger.rb', line 50

def self.sync!
  return if @processor.is_a?(SyncProcessor)

  @processor = SyncProcessor.new(@processor&.appenders)
end

.sync? ⇒ Boolean

Running without the background logging thread?

Returns:

  • (Boolean)


57
58
59
# File 'lib/semantic_logger/logger.rb', line 57

def self.sync?
  processor.is_a?(SyncProcessor)
end

.unsubscribe(subscriber) ⇒ Object

Removes the on_log subscriber.



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

def self.unsubscribe(subscriber)
  @subscribers&.delete(subscriber)
  @ractor_subscribers = (@ractor_subscribers - [subscriber]).freeze if @ractor_subscribers
end

Instance Method Details

#log(log, message = nil, progname = nil) ⇒ 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.



91
92
93
94
95
96
97
98
# File 'lib/semantic_logger/logger.rb', line 91

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

  Logger.call_subscribers(log)

  Logger.processor.log(log)
end