Parent

Logging::Config::YamlConfigurator

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

Public Class Methods

load( file, key = 'logging_config' ) click to toggle source

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.

    # File lib/logging/config/yaml_configurator.rb, line 24
24:       def load( file, key = 'logging_config' )
25:         io, close = nil, false
26:         case file
27:         when String
28:           io = File.open(file, 'r')
29:           close = true
30:         when IO
31:           io = file
32:         else
33:           raise Error, 'expecting a filename or a File'
34:         end
35: 
36:         begin
37:           new(io, key).load
38:         ensure
39:           io.close if close
40:         end
41:         nil
42:       end
new( io, key ) click to toggle source

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.

    # File lib/logging/config/yaml_configurator.rb, line 52
52:     def initialize( io, key  )
53:       YAML.load_documents(io) do |doc|
54:         @config = doc[key]
55:         break if @config.instance_of?(Hash)
56:       end
57: 
58:       unless @config.instance_of?(Hash)
59:         raise Error, "Key '#{key}' not defined in YAML configuration"
60:       end
61:     end

Public Instance Methods

appender( config ) click to toggle source

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.

     # File lib/logging/config/yaml_configurator.rb, line 153
153:     def appender( config )
154:       return if config.nil?
155:       config = config.dup
156: 
157:       type = config.delete('type')
158:       raise Error, 'Appender type not given' if type.nil?
159: 
160:       name = config.delete('name')
161:       raise Error, 'Appender name not given' if name.nil?
162: 
163:       config['layout'] = layout(config.delete('layout'))
164: 
165:       clazz = ::Logging::Appenders.const_get type
166:       clazz.new(name, config)
167:     end
appenders( ary ) click to toggle source

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

     # File lib/logging/config/yaml_configurator.rb, line 112
112:     def appenders( ary )
113:       return if ary.nil?
114: 
115:       ary.each {|h| appender(h)}
116:     end
layout( config ) click to toggle source

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.

     # File lib/logging/config/yaml_configurator.rb, line 177
177:     def layout( config )
178:       return if config.nil?
179:       config = config.dup
180: 
181:       type = config.delete('type')
182:       raise Error, 'Layout type not given' if type.nil?
183: 
184:       clazz = ::Logging::Layouts.const_get type
185:       clazz.new config
186:     end
load click to toggle source

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

    # File lib/logging/config/yaml_configurator.rb, line 69
69:     def load
70:       pre_config @config['pre_config']
71:       ::Logging::Logger[:root]  # ensures the log levels are defined
72:       appenders @config['appenders']
73:       loggers @config['loggers']
74:     end
loggers( ary ) click to toggle source

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

     # File lib/logging/config/yaml_configurator.rb, line 124
124:     def loggers( ary )
125:       return if ary.nil?
126: 
127:       ary.each do |config|
128:         name = config['name']
129:         raise Error, 'Logger name not given' if name.nil?
130: 
131:         l = Logging::Logger.new name
132:         l.level = config['level'] if config.has_key?('level')
133:         l.additive = config['additive'] if l.respond_to? :additive=
134:         l.trace = config['trace'] if l.respond_to? :trace=
135: 
136:         if config.has_key?('appenders')
137:           l.appenders = config['appenders'].map {|n| ::Logging::Appenders[n]}
138:         end
139:       end
140:     end
pre_config( config ) click to toggle source

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

     # File lib/logging/config/yaml_configurator.rb, line 82
 82:     def pre_config( config )
 83:       # if no pre_config section was given, just create an empty hash
 84:       # we do this to ensure that some logging levels are always defined
 85:       config ||= Hash.new
 86: 
 87:       # define levels
 88:       levels = config['define_levels']
 89:       ::Logging.init(levels) unless levels.nil?
 90: 
 91:       # format as
 92:       format = config['format_as']
 93:       ::Logging.format_as(format) unless format.nil?
 94: 
 95:       # backtrace
 96:       value = config['backtrace']
 97:       ::Logging.backtrace(value) unless value.nil?
 98: 
 99:       # grab the root logger and set the logging level
100:       root = ::Logging::Logger.root
101:       if config.has_key?('root')
102:         root.level = config['root']['level']
103:       end
104:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.