Module: Hatchet::LevelManager
- Included in:
- Configuration, LoggerAppender
- Defined in:
- lib/hatchet/level_manager.rb
Overview
Public: Module for managing the configuration of log levels on a class or module level. Useful when you are creating custom appenders.
Constant Summary collapse
- LEVELS =
Internal: All the possible levels of log filter in order of severity.
[:debug, :info, :warn, :error, :fatal, :off]
Instance Method Summary collapse
-
#clear_levels_cache! ⇒ Object
Internal: Removes the caching Hash so that it will be re-initialized.
-
#default_level ⇒ Object
Internal: Returns the default level of the configuration.
-
#enabled?(level, context) ⇒ Boolean
Internal: Returns true if the appender is configured to log messages of the given level within the given context, otherwise returns false.
-
#level(level, context = nil) ⇒ Object
Public: Set the lowest level of message to log for the given context.
-
#levels ⇒ Object
Public: Returns the Hash containing the log level configuration.
-
#levels=(levels) ⇒ Object
Public: Sets the Hash containing the log level configuration.
-
#levels_cache ⇒ Object
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.
Instance Method Details
#clear_levels_cache! ⇒ Object
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.
92 93 94 |
# File 'lib/hatchet/level_manager.rb', line 92 def clear_levels_cache! @_levels_cache = nil end |
#default_level ⇒ Object
Internal: Returns the default level of the configuration.
49 50 51 |
# File 'lib/hatchet/level_manager.rb', line 49 def default_level self.levels[nil] end |
#enabled?(level, context) ⇒ Boolean
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.
62 63 64 65 66 67 68 69 |
# File 'lib/hatchet/level_manager.rb', line 62 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 |
#level(level, context = nil) ⇒ Object
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.
41 42 43 44 45 |
# File 'lib/hatchet/level_manager.rb', line 41 def level(level, context = nil) context = context.to_s unless context.nil? self.levels[context] = level clear_levels_cache! end |
#levels ⇒ Object
Public: Returns the Hash containing the log level configuration.
16 17 18 |
# File 'lib/hatchet/level_manager.rb', line 16 def levels @levels ||= {} end |
#levels=(levels) ⇒ Object
Public: Sets the Hash containing the log level configuration.
levels - The Hash to use as the log level configuration.
Returns nothing.
26 27 28 29 |
# File 'lib/hatchet/level_manager.rb', line 26 def levels=(levels) @levels = levels clear_levels_cache! end |
#levels_cache ⇒ Object
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.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hatchet/level_manager.rb', line 75 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 |