Module: InjectedLogger::Delegator

Defined in:
lib/injectedlogger/delegator.rb

Class Method Summary collapse

Class Method Details

.delegate_levels(on:, from:, prefix:, extra_levels: [], old_levels: [], fallback: UNKNOWN, info: INFO) ⇒ Object

creates methods in klass according to the supported levels in the object specified. (levels are supposed to be methods)

Arguments:

on: class which we’ll create methods on from: underlying logger object, which responds to some levels prefix: prefix log messages with this string extra_levels: extra levels we want to use from klass delegating to logger old_levels: the old levels that the class was delegating previously fallback: not required, suggested fallback level for non-native lvls info: not required, suggested level usable for information

Returns a hash with information about supported and fallback levels:

native: levels natively supported by the underlying object nonnative: non-native levels, callable only if there is fallback fallback: (if existing) native level used as fallback for others supported: supported levels, some maybe via fallback to native ones info: level the caller can use to give info (can be nil)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/injectedlogger/delegator.rb', line 25

def delegate_levels(on:, from:, prefix:, extra_levels: [],
                    old_levels: [], fallback: UNKNOWN, info: INFO)
  self.logger = from
  self.klass = on
  self.prefix = prefix
  supp, unsupp = add_level_methods(extra_levels)
  { native: supp,
    nonnative: unsupp,
    info: preferred_lvl(supp, info) }.
  tap do |level_info|
    level_info.merge!(
      if fallback and unsupp.any?
        flvl = preferred_lvl(supp, fallback)
        add_as_fallback(unsupp, flvl)
        { fallback: flvl, supported: supp + unsupp }
      else
        { supported: supp }
      end)
    (old_levels - level_info[:supported]).each { |lvl| remove_level lvl }
  end
end