Ruby:未初始化常量 Log4r::DEBUG (NameError) 问题

发布于 2024-11-03 09:53:20 字数 650 浏览 5 评论 0原文

在 Ruby 中使用 log4r 时,我编写了一个类似于以下内容的配置文件:

  require 'rubygems'
  require 'log4r'
  require 'log4r/outputter/datefileoutputter' 
  SERVICE_LOG = {
    :log_name         => 'service',
    :log_file         => 'service.log',
    :log_level        => Log4r::DEBUG, 
    :message_pattern  => "[%-5l %d] %C: %M",
    :date_pattern     => "%Y-%m-%d %H:%M:%S"
  }

当我运行它时,它抛出了以下异常:

 C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant Log4r::DEBUG (NameError)

为什么这样做?

While using log4r in Ruby, I wrote a configuration file similar to the following:

  require 'rubygems'
  require 'log4r'
  require 'log4r/outputter/datefileoutputter' 
  SERVICE_LOG = {
    :log_name         => 'service',
    :log_file         => 'service.log',
    :log_level        => Log4r::DEBUG, 
    :message_pattern  => "[%-5l %d] %C: %M",
    :date_pattern     => "%Y-%m-%d %H:%M:%S"
  }

when I ran it, it threw out the following exception:

 C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant Log4r::DEBUG (NameError)

Why did it do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-11-10 09:53:20

有点有点奇怪。您需要先创建一个 logger 实例,然后才能访问日志级别常量。以下是它在 irb 上的外观:

>> require "log4r"
=> true
>> Log4r::DEBUG
NameError: uninitialized constant Log4r::DEBUG
    from (irb):2
>> Log4r::Logger.root
=> #<Log4r::RootLogger:0x101737948 @outputters=[], @level=0>
>> Log4r::DEBUG
=> 1
>>

为了支持自定义级别日志级别仅在加载实例时加载(这是否是正确的方法存在争议)。

这是实际加载级别的代码(从 RootLogger#instance 调用):

Log4r.define_levels(*Log4rConfig::LogLevels)

因此在您的代码中您可以这样称呼它:

require 'rubygems'
require 'log4r'
require 'log4r/outputter/datefileoutputter' 
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
SERVICE_LOG = {
  :log_name         => 'service',
  :log_file         => 'service.log',
  :log_level        => Log4r::DEBUG, 
  :message_pattern  => "[%-5l %d] %C: %M",
  :date_pattern     => "%Y-%m-%d %H:%M:%S"
}

This is a little weird. You need to create a logger instance, before you can access the log level constants. Here is how it looks on irb:

>> require "log4r"
=> true
>> Log4r::DEBUG
NameError: uninitialized constant Log4r::DEBUG
    from (irb):2
>> Log4r::Logger.root
=> #<Log4r::RootLogger:0x101737948 @outputters=[], @level=0>
>> Log4r::DEBUG
=> 1
>>

In order to support custom levels the log levels are loaded only when the instance is loaded (arguable whether this is the right approach).

This is the code that actually loads the levels (called from the RootLogger#instance):

Log4r.define_levels(*Log4rConfig::LogLevels)

So in your code you can call it so:

require 'rubygems'
require 'log4r'
require 'log4r/outputter/datefileoutputter' 
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
SERVICE_LOG = {
  :log_name         => 'service',
  :log_file         => 'service.log',
  :log_level        => Log4r::DEBUG, 
  :message_pattern  => "[%-5l %d] %C: %M",
  :date_pattern     => "%Y-%m-%d %H:%M:%S"
}
与往事干杯 2024-11-10 09:53:20

这些常数似乎不再存在了。我发现的是:

>> Log4r::Log4rConfig.const_get :LogLevels 
#=> ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"]

也许看看他们的示例(例如 requireinclude Log4r):

http://log4r.rubyforge.org/manual.html#outofbox

These constants don't seem to exist anymore. What I found was this though:

>> Log4r::Log4rConfig.const_get :LogLevels 
#=> ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"]

Maybe have a look at their examples (e.g. require and include Log4r):

http://log4r.rubyforge.org/manual.html#outofbox

难理解 2024-11-10 09:53:20

也许你可以尝试这样的事情

require 'rubygems'
require 'log4r'

L4R = Log4r::Logger.new("Logger")
Log4r::FileOutputter.new('service',
                         :filename=>"service.log",
                         :level=>Log4r::DEBUG)
L4R.add('service')

Perhaps you could try something like this

require 'rubygems'
require 'log4r'

L4R = Log4r::Logger.new("Logger")
Log4r::FileOutputter.new('service',
                         :filename=>"service.log",
                         :level=>Log4r::DEBUG)
L4R.add('service')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文