我正在为watir脚本创建日志文件,但我收到错误消息

时间:2011-11-07 08:46:14

标签: ruby

下面的代码保存为logger.rb,然后根据需要将该文件添加到脚本中

require "log4r"
include Log4r

$mylog = Logger.new 'mylog'
$mylog.outputters = Outputter.stdout
$file = FileOutputter.new(
'fileOutputter', :filename => 'D:\WATIRScript\Log\filename',:trunc => false)
$format = PatternFormatter.new( :pattern => "[%l] %d :: %m")

$file.formatter = $format
# log level order is DEBUG < INFO < WARN < ERROR < FATAL
$mylog.level = Log4r::INFO
$mylog.add($file)

运行脚本时出现以下错误

D:/logger.rb:4:in `<top (required)>': undefined local variable or method `Outputter' for main:Object (NameError)
from C:/ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

1 个答案:

答案 0 :(得分:0)

我没有看到你的log4r.yml配置文件。从错误消息,我认为您的配置文件包含问题。

这是一个配置文件供您参考,请注意“输出器”部分:

# save this file as: log4r.yml
log4r_config:

  # define all loggers ...
  loggers:
    - name      : production
      level     : WARN
      trace     : 'false'
      outputters :
      - datefile
    - name      : development
      level     : DEBUG
      trace     : 'true'
      outputters :
      - datefile

  # define all outputters (incl. formatters)
  outputters:
  - type: DateFileOutputter
    name: datefile
    dirname: "log"  # make sure this folder exists
    file: "my_log" # this will be overrided by rails' config.
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter

您可以像这样使用此配置文件:

require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/datefileoutputter'
include Log4r

log4r_config= YAML.load_file(File.join(File.dirname(__FILE__),"log4r.yml"))
YamlConfigurator.decode_yaml( log4r_config['log4r_config'] )
logger = Log4r::Logger["development"]
logger.debug "hi there, see me ? "

您将收到log4r消息。

在Rails环境中查看此链接:How to configure Log4r with Rails 3.0.x?