Class: SmartLoggerWrapper

Inherits:
Logger
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/smart_logger_wrapper.rb,
lib/smart_logger_wrapper/options.rb,
lib/smart_logger_wrapper/version.rb,
lib/smart_logger_wrapper/options/to.rb,
lib/smart_logger_wrapper/utils/path.rb,
lib/smart_logger_wrapper/options/base.rb,
lib/smart_logger_wrapper/utils/backtrace.rb,
lib/smart_logger_wrapper/options/with_position.rb,
lib/smart_logger_wrapper/options/append_backtrace.rb

Defined Under Namespace

Modules: Options, Utils

Constant Summary collapse

LOGGER_SHORTCUT_OFFSET =
3
SEVERITY_MAPPING =
{
  debug:   DEBUG,
  info:    INFO,
  warn:    WARN,
  error:   ERROR,
  fatal:   FATAL,
  unknown: UNKNOWN
}.freeze
DELEGETING_METHODS =
i(<< reopen close log add level debug? level= progname datetime_format= datetime_format formatter sev_threshold sev_threshold= info? warn? error? fatal? progname= formatter=)
VERSION =
"0.4.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = Logger.new(STDOUT), *loggers, **options) ⇒ SmartLoggerWrapper

Returns a new instance of SmartLoggerWrapper.



22
23
24
25
26
27
28
# File 'lib/smart_logger_wrapper.rb', line 22

def initialize(logger = Logger.new(STDOUT), *loggers, **options)
  @loggers = [logger, *loggers].freeze
  @options = options.freeze
  @offset = LOGGER_SHORTCUT_OFFSET
  @_loggers_cache = {}
  @_loggers_with_offset_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/smart_logger_wrapper.rb', line 97

def method_missing(method_name, *args, &block)
  if Options.defined_option?(method_name)
    # If there is an defined option with the same name as the method name, return a new logger with the option.
    arg = args.first
    @_loggers_cache[method_name] = {} unless @_loggers_cache.include?(method_name)
    new_logger = @_loggers_cache[method_name][arg] ||= clone.tap do |cloned|
      cloned.overwrite_options(method_name => arg)
    end
    return block.(new_logger) if block_given?
    new_logger
  else
    super
  end
end

Instance Attribute Details

#loggersObject (readonly)

Returns the value of attribute loggers.



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

def loggers
  @loggers
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#format_message(*args) ⇒ Object



62
63
64
# File 'lib/smart_logger_wrapper.rb', line 62

def format_message(*args)
  loggers.first.send(:format_message, *args)
end

#overwrite_options(_options) ⇒ Object



58
59
60
# File 'lib/smart_logger_wrapper.rb', line 58

def overwrite_options(_options)
  @options = options.merge(_options).freeze
end

#with_offset(_offset) ⇒ Object



52
53
54
55
56
# File 'lib/smart_logger_wrapper.rb', line 52

def with_offset(_offset)
  @_loggers_with_offset_cache[_offset] ||= clone.tap do |logger_with_offset|
    logger_with_offset.instance_variable_set(:@offset, _offset)
  end
end