Class: LogSinks::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/log_sinks/logger.rb,
lib/log_sinks/ruby_logger_adapter.rb

Overview

Logger is a object that captures log event and forward that to event sinks.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Logger

Returns a new instance of Logger.



24
25
26
27
28
29
# File 'lib/log_sinks/logger.rb', line 24

def initialize(name)
  validate_name(name)

  @name = name
  @level = ::LogSinks::Level[:info]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/log_sinks/logger.rb', line 22

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



15
16
17
18
19
# File 'lib/log_sinks/logger.rb', line 15

def [](name)
  ::LogSinks::Repository.instance.get_or_add(name) do |logger_name|
    Logger.new(logger_name)
  end
end

Instance Method Details

#as_ruby_logger(progname: nil) ⇒ Object



11
12
13
# File 'lib/log_sinks/ruby_logger_adapter.rb', line 11

def as_ruby_logger(progname: nil)
  RubyLoggerAdapter.new(self, progname)
end

#level=(level) ⇒ Object



35
36
37
38
39
40
# File 'lib/log_sinks/logger.rb', line 35

def level=(level)
  @level = case level
           when String, Symbol then ::LogSinks::Level[level.to_sym]
           when ::LogSinks::Level then level
           end
end

#level?(level) ⇒ Boolean

Returns:

  • (Boolean)


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

def level?(level)
  @level.val <= level.val
end

#log_event(level, msg = nil, meta: nil, error: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/log_sinks/logger.rb', line 54

def log_event(level, msg = nil, meta: nil, error: nil)
  return false unless level?(level)

  msg = yield if msg.nil? && block_given?
  meta ||= {}
  meta[:error] = error unless error.nil?
  meta.merge!(MDC.items) if MDC.any?
  event = ::LogSinks::LogEvent.new(Time.now, level, @name, msg, meta: meta)
  dispatch_event event
end