Skip to content

Commit

Permalink
Merge pull request colbygk#1 from jablan/master
Browse files Browse the repository at this point in the history
patch for yamlconfigurator
  • Loading branch information
Colby Gutierrez-Kraybill committed May 17, 2011
2 parents a036852 + 4fb925d commit 3489cde
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
46 changes: 24 additions & 22 deletions lib/log4r/yamlconfigurator.rb
Expand Up @@ -119,13 +119,13 @@ def self.decode_outputter( op)
end

formatter = decode_formatter( op['formatter'])
# build the eval string
buff = "Outputter[name] = #{type}.new name"
buff += ",:level=>#{LNAMES.index(level)}" unless level.nil?
buff += ",:formatter=>formatter" unless formatter.nil?
params = decode_hash_params( op)
buff += "," + params.join(',') if params.size > 0
begin eval buff

opts = {}
opts[:level] = LNAMES.index(level) unless level.nil?
opts[:formatter] = formatter unless formatter.nil?
opts.merge!(decode_hash_params(op))
begin
Outputter[name] = Log4r.const_get(type).new name, opts
rescue Exception => ae
raise ConfigError,
"Problem creating outputter: #{ae.message}", caller[1..-3]
Expand All @@ -138,8 +138,8 @@ def self.decode_formatter( fo)
return nil if fo.nil?
type = fo['type']
raise ConfigError, "Formatter missing type", caller[1..-4] if type.nil?
buff = "#{type}.new " + decode_hash_params( fo).join(',')
begin return eval( buff)
begin
return Log4r.const_get(type).new(decode_hash_params(fo))
rescue Exception => ae
raise ConfigError,
"Problem creating outputter: #{ae.message}", caller[1..-4]
Expand All @@ -149,23 +149,25 @@ def self.decode_formatter( fo)
ExcludeParams = %w{formatter level name type only_at}

# Does the fancy parameter to hash argument transformation
def self.decode_hash_params( ph)
buff = []
ph.each{ |name, value|
next if ExcludeParams.include? name
buff << ":" + name + "=>" + paramsub( value)
}
buff
def self.decode_hash_params(ph)
case ph
when Hash
ph.inject({}){|a,(k,v)| a[k] = self.decode_hash_params(v); a}
when Array
ph.map{|v| self.decode_hash_params(v)}
when String
self.paramsub(ph)
else
ph
end
end

# Substitues any #{foo} in the YAML with Parameter['foo']
def self.paramsub( str)
return nil if str.nil?
return nil if str.class != String
def self.paramsub(str)
@@params.each {|param, value|
str.sub!( '#{' + param + '}', value)
str = str.sub("\#{#{param}}", value)
}
"'" + str + "'"
str
end

def self.decode_logger( lo)
Expand Down
42 changes: 42 additions & 0 deletions tests/testyaml.rb
@@ -0,0 +1,42 @@
$: << File.join('..','lib') # path if log4r is not installed
require "test/unit"
require 'log4r'
require 'log4r/yamlconfigurator'
include Log4r

# Define a custom outputter that allows arrays in configuration hash
module Log4r
class TestYamlOutputter < Outputter
# expose array parameter
attr_reader :array_param

def initialize(name, hash = {})
@array_param = hash['array_param']
end
end
end


class TestYaml < Test::Unit::TestCase

def setup
@cfg = YamlConfigurator # shorthand
@cfg['CUSTOM_DOMAIN'] = 'bar.com'
end

def test_injection
assert_nothing_raised("Exception injected") do
@cfg.load_yaml_file(File.join(File.dirname(__FILE__),'testyaml_injection.yaml'))
end
end

def test_arrays
assert_nothing_raised("Parser couldn't handle arrays in YAML") do
@cfg.load_yaml_file(File.join(File.dirname(__FILE__),'testyaml_arrays.yaml'))
end
log = Logger['mylogger']
assert_instance_of(Array, log.outputters.first.array_param, 'Array not loaded properly from YAML')
assert_equal('wilma@bar.com', log.outputters.first.array_param[2], '#{}-style parameter interpolation doesn\'t work properly in arrays')
end
end

25 changes: 25 additions & 0 deletions tests/testyaml_arrays.yaml
@@ -0,0 +1,25 @@
log4r_config:

# define all loggers ...
loggers:
- name : mylogger
level : INFO
additive : 'false'
trace : 'false'
outputters:
- testyaml

# define all outputters (incl. formatters)
outputters:
- type : TestYamlOutputter
name : testyaml
level : INFO
array_param:
- fred@foo.com
- barney@foo.com
- 'wilma@#{CUSTOM_DOMAIN}'
formatter:
date_pattern: '%y%m%d %H:%M:%S'
pattern : '%d %l: %m '
type : PatternFormatter

22 changes: 22 additions & 0 deletions tests/testyaml_injection.yaml
@@ -0,0 +1,22 @@
log4r_config:

# define all loggers ...
loggers:
- name : mylogger
level : INFO
additive : 'false'
trace : 'false'
outputters:
- stderr

# define all outputters (incl. formatters)
outputters:
- type : StderrOutputter
name : stderr
level : INFO
crash : "'; raise Exception #"
formatter:
date_pattern: '%y%m%d %H:%M:%S'
pattern : '%d %l: %m '
type : PatternFormatter

0 comments on commit 3489cde

Please sign in to comment.