Class: Logging::Config::YamlConfigurator

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/config/yaml_configurator.rb

Overview

The YamlConfigurator class is used to configure the Logging framework using information found in a YAML file.

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, key) ⇒ YamlConfigurator

call-seq:

YamlConfigurator.new( io, key )

Creates a new YAML configurator that will load the Logging configuration from the given io stream. The configuration will be loaded from the given key in the YAML stream.



62
63
64
65
66
67
68
69
70
71
# File 'lib/logging/config/yaml_configurator.rb', line 62

def initialize( io, key  )
  YAML.load_documents(io) do |doc|
    @config = doc[key]
    break if @config.instance_of?(Hash)
  end

  unless @config.instance_of?(Hash)
    raise Error, "Key '#{key}' not defined in YAML configuration"
  end
end

Class Method Details

.load(file, key = 'logging_config') ⇒ Object

call-seq:

YamlConfigurator.load( file, key = 'logging_config' )

Load the given YAML file and use it to configure the Logging framework. The file can be either a filename, and open File, or an IO object. If it is the latter two, the File / IO object will not be closed by this method.

The configuration will be loaded from the given key in the YAML stream.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logging/config/yaml_configurator.rb', line 28

def load( file, key = 'logging_config' )
  io, close = nil, false
  case file
  when String
    ext = File.extname(file)
    if ext == 'yml' || ext == 'yaml'
      io = File.open(file, 'r')
      close = true
    else
      io = StringIO.new(file)
      close = true
    end
  when IO
    io = file
  else
    raise Error, 'expecting a filename or a File'
  end

  begin
    new(io, key).load
  ensure
    io.close if close
  end
  nil
end

Instance Method Details

#appender(config) ⇒ Object

call-seq:

appender( config )

Creates a new Appender based on the given config options (a hash). The type of Appender created is determined by the ‘type’ option in the config. The remaining config options are passed to the Appender initializer.

The config options can also contain a ‘layout’ option. This should be another set of options used to create a Layout for this Appender.

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/logging/config/yaml_configurator.rb', line 158

def appender( config )
  return if config.nil?
  config = config.dup

  type = config.delete('type')
  raise Error, 'Appender type not given' if type.nil?

  name = config.delete('name')
  raise Error, 'Appender name not given' if type.nil?

  config['layout'] = layout(config.delete('layout'))

  clazz = ::Logging::Appenders.const_get type
  clazz.new(name, config)
end

#appenders(ary) ⇒ Object

call-seq:

appenders( ary )

Given an array of Appender configurations, this method will iterate over each and create the Appender(s).



117
118
119
120
121
# File 'lib/logging/config/yaml_configurator.rb', line 117

def appenders( ary )
  return if ary.nil?

  ary.each {|h| appender(h)}
end

#layout(config) ⇒ Object

call-seq:

layout( config )

Creates a new Layout based on the given config options (a hash). The type of Layout created is determined by the ‘type’ option in the config. The remaining config options are passed to the Layout initializer.

Raises:



182
183
184
185
186
187
188
189
190
191
# File 'lib/logging/config/yaml_configurator.rb', line 182

def layout( config )
  return if config.nil?
  config = config.dup

  type = config.delete('type')
  raise Error, 'Layout type not given' if type.nil?

  clazz = ::Logging::Layouts.const_get type
  clazz.new config
end

#loadObject

call-seq:

load

Loads the Logging configuration from the data loaded from the YAML file.



79
80
81
82
83
# File 'lib/logging/config/yaml_configurator.rb', line 79

def load
  pre_config @config['pre_config']
  appenders @config['appenders']
  loggers @config['loggers']
end

#loggers(ary) ⇒ Object

call-seq:

loggers( ary )

Given an array of Logger configurations, this method will iterate over each and create the Logger(s).



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/logging/config/yaml_configurator.rb', line 129

def loggers( ary )
  return if ary.nil?

  ary.each do |config|
    name = config['name']
    raise Error, 'Logger name not given' if name.nil?

    l = Logging::Logger.new name
    l.level = config['level'] if config.has_key?('level')
    l.additive = config['additive'] if l.respond_to? :additive=
    l.trace = config['trace'] if l.respond_to? :trace=

    if config.has_key?('appenders')
      l.appenders = config['appenders'].map {|n| ::Logging::Appender[n]}
    end
  end
end

#pre_config(config) ⇒ Object

call-seq:

pre_config( config )

Configures the logging levels, object format style, and root logging level.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/logging/config/yaml_configurator.rb', line 91

def pre_config( config )
  # if no pre_config section was given, just create an empty hash
  # we do this to ensure that some logging levels are always defined
  config ||= Hash.new

  # define levels
  levels = config['define_levels']
  ::Logging.init(levels) unless levels.nil?

  # format as
  format = config['format_as']
  ::Logging.format_as(format) unless format.nil?

  # grab the root logger and set the logging level
  root = ::Logging::Logger.root
  if config.has_key?('root')
    root.level = config['root']['level']
  end
end