class Logsly::Logging182::Config::YamlConfigurator
The YamlConfigurator
class is used to configure the Logsly::Logging182
framework using information found in a YAML file.
Public Class Methods
Load the given YAML file and use it to configure the Logsly::Logging182
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/logsly/logging182/config/yaml_configurator.rb, line 24 def load( file, key = 'logging_config' ) io, close = nil, false case file when String io = File.open(file, 'r') close = true 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
Creates a new YAML configurator that will load the Logsly::Logging182
configuration from the given io stream. The configuration will be loaded from the given key in the YAML stream.
# File lib/logsly/logging182/config/yaml_configurator.rb, line 52 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
Public Instance Methods
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/logsly/logging182/config/yaml_configurator.rb, line 153 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 name.nil? config['layout'] = layout(config.delete('layout')) clazz = ::Logsly::Logging182::Appenders.const_get type clazz.new(name, config) end
Given an array of Appender
configurations, this method will iterate over each and create the Appender(s).
# File lib/logsly/logging182/config/yaml_configurator.rb, line 112 def appenders( ary ) return if ary.nil? ary.each {|h| appender(h)} end
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/logsly/logging182/config/yaml_configurator.rb, line 177 def layout( config ) return if config.nil? config = config.dup type = config.delete('type') raise Error, 'Layout type not given' if type.nil? clazz = ::Logsly::Logging182::Layouts.const_get type clazz.new config end
Loads the Logsly::Logging182
configuration from the data loaded from the YAML file.
# File lib/logsly/logging182/config/yaml_configurator.rb, line 69 def load pre_config @config['pre_config'] ::Logsly::Logging182::Logger[:root] # ensures the log levels are defined appenders @config['appenders'] loggers @config['loggers'] end
Given an array of Logger
configurations, this method will iterate over each and create the Logger(s).
# File lib/logsly/logging182/config/yaml_configurator.rb, line 124 def loggers( ary ) return if ary.nil? ary.each do |config| name = config['name'] raise Error, 'Logger name not given' if name.nil? l = Logsly::Logging182::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| ::Logsly::Logging182::Appenders[n]} end end end
Configures the logging levels, object format style, and root logging level.
# File lib/logsly/logging182/config/yaml_configurator.rb, line 82 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'] ::Logsly::Logging182.init(levels) unless levels.nil? # format as format = config['format_as'] ::Logsly::Logging182.format_as(format) unless format.nil? # backtrace value = config['backtrace'] ::Logsly::Logging182.backtrace(value) unless value.nil? # grab the root logger and set the logging level root = ::Logsly::Logging182::Logger.root if config.has_key?('root') root.level = config['root']['level'] end end