class Hatchet::Configuration
Public: Class for configuring Hatchet
.
Attributes
Public: The Array of configured appenders.
Public Class Methods
Internal: Creates a new configuration.
Creates the levels Hash with a default logging level of info.
# File lib/hatchet/configuration.rb, line 17 def initialize @formatter = nil reset! end
Public Instance Methods
Public: Adds backtrace filters provided in the form of a Hash.
Each line of the backtrace starting with a key is replaced by its corresponding value.
Example
configuration.configure do |config| config.backtrace_filter '/applications/my_app/releases/current' => '$ROOT' end
Will filter a backtrace line like:
/applications/my_app/releases/current/lib/example.rb:42:in `main'
Into:
$ROOT/lib/example.rb:42:in `main'
Returns nothing.
# File lib/hatchet/configuration.rb, line 43 def backtrace_filter(filters = nil) @backtrace_filters.merge!(filters) if filters @backtrace_filters end
Internal: Removes the caching Hash of every appender so that they will all be re-initialized.
Used when a change to logging levels is made so that the caches will not contain stale values.
# File lib/hatchet/configuration.rb, line 128 def clear_levels_cache! appenders.each(&:clear_levels_cache!) end
Public: Yields the configuration object to the given block to make it tidier when setting multiple values against a referenced configuration.
block - Mandatory block which receives a Configuration
object that can be
used to setup Hatchet.
Once the block returns each of the configured appenders has its formatter set to the default formatter if they have one and one is not already set, and its levels Hash is set to the shared levels Hash if an explicit one has not been provided.
Example
configuration.configure do |config| # Set the level to use unless overridden (defaults to :info) config.level :info # Set the level for a specific class/module and its children config.level :debug, 'Namespace::Something::Nested' # Add as many appenders as you like config.appenders << Hatchet::LoggerAppender.new do |appender| # Set the logger that this is wrapping (required) appender.logger = Logger.new('log/test.log') end end
Returns nothing.
# File lib/hatchet/configuration.rb, line 111 def configure yield self # Ensure every appender has a formatter and a level configuration. # appenders.each do |appender| appender.formatter ||= @formatter if appender.respond_to? 'formatter=' appender.levels = @levels if appender.levels.empty? end end
Public: Returns the default formatter given to the appenders that have not had their formatter explicitly set.
If not otherwise set, will be a StandardFormatter
.
# File lib/hatchet/configuration.rb, line 55 def formatter @formatter.formatter end
Public: Sets the default formatter given to the appenders that have not had their formatter explicitly set.
# File lib/hatchet/configuration.rb, line 62 def formatter=(formatter) @formatter.formatter = formatter end
Public: Resets the configuration's internal state to the defaults.
# File lib/hatchet/configuration.rb, line 68 def reset! @backtrace_filters = {} @levels = { nil => :info } @appenders = [] # If a DelegatingFormatter has already been set up replace its # formatter, otherwise create a new one. # if @formatter @formatter.formatter = StandardFormatter.new else @formatter = DelegatingFormatter.new(StandardFormatter.new) end end