Bugs: Browse | Submit New | Admin

[#29621] Subsitution fails when value is integer, while configuring UDPOutputter's port value via a YAML config file.

Date:
2012-07-29 21:33
Priority:
3
Submitted By:
James Scott Jr (skoona)
Assigned To:
Colby Gutierrez-Kraybill (colbygk)
Category:
None
State:
Open
Summary:
Subsitution fails when value is integer, while configuring UDPOutputter's port value via a YAML config file.

Detailed description
The Issue:
log4r-1.1.0/lib/yamlconfigurator.rb:166:def self.paramsub(str) incorrectly expects the replacement value to be a string
and not an integer; and provides facilities (sub!) to overwrite the placeholder with string values only. 
The UDPOutputter()'s :port param needs an integer as its value.  In the YAML I used port: #{log_port} and setup a external
Parameter I though would place the integer 8071 inlace of #{log_port}, only to receive the following error message.
I'm offering a very short fix inline to this bug report.

error message:  

"Log4r::ConfigError: Problem creating outputter: can't convert Fixnum into String org/jruby/RubyString.java:2755:in
`sub!'"

I'm using version 1.1.10 with Rails 3.2.7 and experienced the above exception.  In particular, I was setting up a ChainSaw
configuration using the UDPOutputter and Log4jXmlFormatter.  UDPOutputter has params of :hostname=String, and :port=Integer.
I wanted these values to come from my central configuration (ruby_config.gem) and decided to use the parameter substitution
feature.

example: <Rails.root>/config/initializers/syslog.rb

#requires.../includes not shown
BMIService::Application.configure do
  cfg = YamlConfigurator

  cfg.[]=("log_file", Rails.env)
  cfg.[]=("log_host", Settings.remote_log.hostname)   # resolves to string 'localhost'
  cfg.[]=("log_port", Settings.remote_log.port)            # resolves to integer 8071

  cfg.load_yaml_file(File.join(File.dirname(__FILE__),"../log4r.yml"))

  Rails.logger = Log4r::Logger[Rails.env]
  config.logger = Log4r::Logger[Rails.env]
end

example: <Rails.root>/config/log4r.yaml  (NOT USING PARAMETERS in the yaml config file.)
# regular yaml config not shown...

  - type: UDPOutputter
    name: udp
    level: DEBUG
    hostname: '#{log_host}'
    port: '#{log_port}'
    formatter:
      date_pattern: '%Y-%m-%d %H:%M:%S.%3N'
      pattern: '%d %l: %m '
      type: Log4jXmlFormatter

Notice: hostname and port's value MUST BE quoted.

Current Log4r Code:
log4r-1.1.0/lib/yamlconfigurator.rb:166

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

Suggested Change or work around:
log4r-1.1.0/lib/yamlconfigurator.rb:166

    # Substitues any #{foo} in the YAML with Parameter['foo']
    def self.paramsub(str)
      @@params.each do |param, value|      # values from params could be String or Integer
        if str.include?(param)
          str = str.sub("\#{#{param}}", value) if value.kind_of?(String) # Handles String Values
          str = value unless value.kind_of?(String)      # Handles Integer values
        end
      end
      str
    end

The change is very short.  I hope to can get what you need from this.  Otherwise let me know what you need.

James, 

Add A Comment: Notepad

Please login


Followup

Message
Date: 2012-07-30 01:57
Sender: Colby Gutierrez-Kraybill

I am currently moving between states!  Will follow up once I
arrive.
Date: 2012-07-29 22:09
Sender: James Scott Jr

this fork exists with the change noted in original bug report.

git://github.com/skoona/log4r.git

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

Field Old Value Date By
assigned_tonone2012-07-29 21:47skoona