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

BASE_OFFSET =
3
NESTED_WRAPPER_OFFSET =
6
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=).freeze
VERSION =
"0.5.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SmartLoggerWrapper.



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

def initialize(logger = Logger.new(STDOUT), *loggers, base_offset: nil, parent: nil, **options)
  @base_offset = base_offset || BASE_OFFSET
  @parent = parent
  @loggers = be_parent_of!(logger, *loggers).freeze
  @options = options.freeze
  @_loggers_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/smart_logger_wrapper.rb', line 122

def method_missing(method_name, *args, &block)
  if root? && Options.defined_option?(method_name)
    # When a root wrapper receives an defined option with the same name as the method name,
    # return a new logger wrapper with the option.
    @_loggers_cache[method_name] = {} unless @_loggers_cache.include?(method_name)
    logger_with_option = @_loggers_cache[method_name][args] ||= with_option(method_name, *args)
    return block.(logger_with_option) if block_given?
    logger_with_option
  else
    super
  end
end

Instance Attribute Details

#base_offsetObject (readonly)

Returns the value of attribute base_offset.



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

def base_offset
  @base_offset
end

#loggersObject (readonly)

Returns the value of attribute loggers.



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

def loggers
  @loggers
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#depthObject



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

def depth
  return 0 if root?
  parent.depth + 1
end

#format_message(severity, datetime, progname, msg) ⇒ Object



66
67
68
# File 'lib/smart_logger_wrapper.rb', line 66

def format_message(severity, datetime, progname, msg)
  loggers.first.send(:format_message, severity, datetime, progname, msg)
end

#format_severity(severity) ⇒ Object



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

def format_severity(severity)
  loggers.first.send(:format_severity, severity)
end

#offsetObject



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

def offset
  @base_offset + depth * NESTED_WRAPPER_OFFSET
end

#with_option(option_name, *args) ⇒ Object



70
71
72
73
# File 'lib/smart_logger_wrapper.rb', line 70

def with_option(option_name, *args)
  new_options = options.merge(option_name => args)
  self.class.new(*loggers, base_offset: base_offset, parent: parent, **new_options)
end