Class: Hatchet::Configuration

Inherits:
Object
  • Object
show all
Includes:
LevelManager
Defined in:
lib/hatchet/configuration.rb

Overview

Public: Class for configuring Hatchet.

Constant Summary

Constants included from LevelManager

LevelManager::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LevelManager

#default_level, #enabled?, #level, #levels, #levels=, #levels_cache

Constructor Details

#initializeConfiguration

Internal: Creates a new configuration.

Creates the levels Hash with a default logging level of info.



18
19
20
21
# File 'lib/hatchet/configuration.rb', line 18

def initialize
  @formatter = nil
  reset!
end

Instance Attribute Details

#appendersObject (readonly)

Public: The Array of configured appenders.



12
13
14
# File 'lib/hatchet/configuration.rb', line 12

def appenders
  @appenders
end

Instance Method Details

#backtrace_filter(filters = nil) ⇒ Object Also known as: backtrace_filters

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.



44
45
46
47
# File 'lib/hatchet/configuration.rb', line 44

def backtrace_filter(filters = nil)
  @backtrace_filters.merge!(filters) if filters
  @backtrace_filters
end

#clear_levels_cache!Object

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.



129
130
131
# File 'lib/hatchet/configuration.rb', line 129

def clear_levels_cache!
  appenders.each(&:clear_levels_cache!)
end

#configure {|_self| ... } ⇒ Object

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.

Yields:

  • (_self)

Yield Parameters:



112
113
114
115
116
117
118
119
120
121
# File 'lib/hatchet/configuration.rb', line 112

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

#formatterObject

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.



56
57
58
# File 'lib/hatchet/configuration.rb', line 56

def formatter
  @formatter.formatter
end

#formatter=(formatter) ⇒ Object

Public: Sets the default formatter given to the appenders that have not had their formatter explicitly set.



63
64
65
# File 'lib/hatchet/configuration.rb', line 63

def formatter=(formatter)
  @formatter.formatter = formatter
end

#reset!Object

Public: Resets the configuration’s internal state to the defaults.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hatchet/configuration.rb', line 69

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