Class: TaggedLogger::TagMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/tagged_logger/tagged_logger.rb

Constant Summary collapse

LEVELS =
{ :debug => 1, :info => 2, :warn => 3, :error => 4, :fatal => 5}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_spec, level = nil) ⇒ TagMatcher

Returns a new instance of TagMatcher.



136
137
138
139
# File 'lib/tagged_logger/tagged_logger.rb', line 136

def initialize(match_spec, level = nil)
  @level = level || :debug
  @match_spec = match_spec
end

Instance Attribute Details

#match_specObject (readonly)

Returns the value of attribute match_spec.



133
134
135
# File 'lib/tagged_logger/tagged_logger.rb', line 133

def match_spec
  @match_spec
end

Class Method Details

.match?(spec, tag) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tagged_logger/tagged_logger.rb', line 150

def self.match?(spec, tag)
  t = tag.to_s
  result = case spec
           when Regexp 
             t =~ spec
           when Class 
             t == spec.name
           when Array 
             spec.any? {|s| match?(s, tag)}
           else
             spec.to_s == t
           end
  return result if result
  self.match?(spec, tag.superclass) if tag.class == Class && tag.superclass != Class
end

Instance Method Details

#above_treshold(level) ⇒ Object



146
147
148
# File 'lib/tagged_logger/tagged_logger.rb', line 146

def above_treshold(level)
  LEVELS[@level] <= LEVELS[level]
end

#match?(tag, level = nil) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/tagged_logger/tagged_logger.rb', line 141

def match?(tag, level = nil)
  return false if level && !above_treshold(level)
  self.class.match?(@match_spec, tag)
end