Ruby:未初始化常量 Log4r::DEBUG (NameError) 问题
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有点有点奇怪。您需要先创建一个 logger 实例,然后才能访问日志级别常量。以下是它在 irb 上的外观:
为了支持自定义级别日志级别仅在加载实例时加载(这是否是正确的方法存在争议)。
这是实际加载级别的代码(从
RootLogger#instance
调用):因此在您的代码中您可以这样称呼它:
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: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
):So in your code you can call it so:
这些常数似乎不再存在了。我发现的是:
也许看看他们的示例(例如
require
和include
Log4r):http://log4r.rubyforge.org/manual.html#outofbox
These constants don't seem to exist anymore. What I found was this though:
Maybe have a look at their examples (e.g.
require
andinclude
Log4r):http://log4r.rubyforge.org/manual.html#outofbox
也许你可以尝试这样的事情
Perhaps you could try something like this