module Hatchet::LevelManager
Public: Module for managing the configuration of log levels on a class or module level. Useful when you are creating custom appenders.
Constants
- LEVELS
Internal: All the possible levels of log filter in order of severity.
Public Instance Methods
Internal: Removes the caching Hash so that it will be re-initialized.
Used when a change to logging levels is made so that the cache will not contain stale values.
# File lib/hatchet/level_manager.rb, line 91 def clear_levels_cache! @_levels_cache = nil end
Internal: Returns the default level of the configuration.
# File lib/hatchet/level_manager.rb, line 48 def default_level self.levels[nil] end
Internal: Returns true if the appender is configured to log messages of the given level within the given context, otherwise returns false.
level - The level of the message. context - The context of the message.
Returns true if the appender is configured to log messages of the given level within the given context, otherwise returns false.
# File lib/hatchet/level_manager.rb, line 61 def enabled?(level, context) lvl = self.levels_cache[context] # Return false if no level is configured. return false unless lvl LEVELS.index(level) >= LEVELS.index(lvl) end
Public: Set the lowest level of message to log for the given context.
level - The lowest level of message to log for the given context. context - The context that level applies to (default: nil).
Setting a level for nil sets the default level for all contexts that have not been specified.
Returns nothing.
# File lib/hatchet/level_manager.rb, line 40 def level(level, context = nil) context = context.to_s unless context.nil? self.levels[context] = level clear_levels_cache! end
Public: Returns the Hash containing the log level configuration.
# File lib/hatchet/level_manager.rb, line 15 def levels @levels ||= {} end
Public: Sets the Hash containing the log level configuration.
levels - The Hash to use as the log level configuration.
Returns nothing.
# File lib/hatchet/level_manager.rb, line 25 def levels=(levels) @levels = levels clear_levels_cache! end
Internal: Returns a lazily duplicated Hash from the levels Hash which is used to store the calculated logging level for specific contexts to make subsequent lookups more efficient.
# File lib/hatchet/level_manager.rb, line 74 def levels_cache @_levels_cache ||= begin new_cache = Hash.new do |hash, key| hash[key] = level_for_context(key) end self.levels.each { |k, v| new_cache[k] = v } # Ensure there is always a default fallback new_cache[nil] = :info unless new_cache.include?(nil) new_cache end end
Private Instance Methods
Private: Returns the minimum active logging level for the given context.
context - The context of the logging call.
Returns the minimum level that a message would be logged at for the given context.
# File lib/hatchet/level_manager.rb, line 104 def level_for_context(context) lvl = self.levels_cache[nil] root = [] context.to_s.split('::').each do |part| root << part path = root.join '::' lvl = self.levels_cache[path] if self.levels_cache.key? path end lvl end