Skip to content

Commit

Permalink
rewritten yamlconfigurator so it doesn't use eval anymore, supporting…
Browse files Browse the repository at this point in the history
… almost arbitrary, nested configuration
  • Loading branch information
Mladen Jablanovic committed Feb 27, 2011
1 parent a036852 commit d052fcd
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions lib/log4r/yamlconfigurator.rb
Original file line number Diff line number Diff line change
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

This comment has been minimized.

Copy link
@wr0ngway

wr0ngway Mar 28, 2012

This breaks use of outputters defined by third parties - e.g. log4r-gelf - its now only looking for types defined directly in Log4r namespace - e.g. Log4r::Gelf::GelfOutputter fails to be resolved, where before it worked just fine. I can change the class name, but maybe this should be more permissive

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
Hash[ph.map{|k,v| [k, self.decode_hash_params(v)]}]
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

0 comments on commit d052fcd

Please sign in to comment.