如何在不与其他log4r记录器冲突的情况下在log4r中定义自己的级别? [英] How can I define my own levels in log4r without a conflict with other log4r-loggers?

查看:99
本文介绍了如何在不与其他log4r记录器冲突的情况下在log4r中定义自己的级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用log4r定义自己的日志级别:

I can define my own log-levels with log4r:

require 'log4r'
require 'log4r/configurator'

# This is how we specify our levels
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"

log = Log4r::Logger.new('custom levels')
p log.levels  #-> ["ALL", "Foo", "Bar", "Baz", "OFF"]
log.add Log4r::StdoutOutputter.new('console')

puts log.foo? #-> true
log.foo "This is foo 1"

log.level = Log4r::Bar
puts log.foo? #->false
log.foo "This is foo 2"

log4r文档说:

此外,在使用Log4r进行任何其他操作之前,应先设置自定义级别,否则将加载默认级别.

Also, custom levels should be set before anything else is done with Log4r, otherwise the default levels will be loaded.

此限制是两个问题的根源:

This restriction is the source of two problems:

  • 只有在未定义记录器的情况下,才能使用Log4r::Configurator.custom_levels设置新级别.
  • 如果我在定义自己的级别之后创建另一个记录器,则会再次获得自定义级别.
  • Setting new levels with Log4r::Configurator.custom_levels works only, if there is no logger defined.
  • If I create another logger after defining my own levels, I get again the custom levels.

示例:

require 'log4r'
require 'log4r/configurator'

#Create a dummy logger
Log4r::Logger.new('dummy')

# Define new levels -> does not work after creation of another logger.
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"
log = Log4r::Logger.new('custom levels')
p log.levels  #-> ["ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF"]

不使用自定义级别.

如果我定义自己的级别,则不可能返回到默认值.

If I define my own levels, I have no possibility to go back to the defaults.

示例:

require 'log4r'
require 'log4r/configurator'

# Define customer levels
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"
Log4r::Logger.new('dummy with custom levels')

#Reset to standard levels ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"] <- this does not work
Log4r::Configurator.custom_levels( *Log4r::Log4rConfig::LogLevels )
log2 = Log4r::Logger.new('log')
p log2.levels #-> ["ALL", "Foo", "Bar", "Baz", "OFF"], but I wanted ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"]

背景:

我想使用自己的类似于Apache : DEBUG INFO NOTICE WARN ERROR CRIT ALERT EMERG.

这样做时,我遇到两个问题:

When I do so, I get two problems:

  • 如果另一个应用程序也使用log4r,则该应用程序没有致命级别. 如果我在应用程序中添加了致命级别,则可以解决此问题.
  • 如果在我的应用程序之前加载了另一个应用程序并创建了记录器, 我的应用程序将失败,因为没有可用的客户级别.
  • If another application uses also log4r, that application has no fatal-level. This can be solved, if I add a fatal-level in my application.
  • If another application is loaded before my application and creates a logger, my application will fail, because my customer levels are not available.

如何创建具有自定义日志级别且没有副作用的记录器 其他记录器?

How can I create a logger with customized log levels and without side effects to other loggers?

推荐答案

自定义级别在整个流程范围内,设置后无法重置.但是可能有一些方法可以实现您想要的.

Custom levels are process-wide and you cannot reset it after it's already set. But there might be some way to achieve what you want.

问题1

必须设置先前的自定义级别是有原因的,并且在设置自定义级别时会创建记录器方法和输出程序方法,因此,如果要删除一些可能导致意外错误的自定义级别,则应格外小心

Previous custom levels must be set for a reason, and logger methods and outputter methods are created during setting up the custom levels, thus you should be careful if you're going to remove some of the custom levels which might cause unexpected errors.

但是,有一种解决方法可以从测试目的. (此方法将log4r重置为原始状态,清除所有现有的记录器和输出器)

However, there is a workaround to reset the custom levels from my fork of Log4r and it's for test purpose. (This method resets log4r to the original state, wipes out all existing loggers and outputters)

问题2

有两种解决冲突的方法:

There are two solutions to resolve the conflicts:

  • 在其他模块之前定义所有自定义级别.并且您必须包括所有自定义级别.例如['Foo', 'Bar', 'Bing', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
  • 如果您不能在其他模块之前完成此操作,请使用上述方法重置log4r并重新定义自定义级别,并在需要时从其他模块加载配置
  • 按照以下方式使用别名方法,并相应地更改log4r配置(例如,将foo替换为debug等)
Log4r::Logger.module_eval %{
  alias_method :foo, :debug
  alias_method :bar, :info
  alias_method :baz, :error
  alias_method :qux, :fatal
}

希望有帮助.

这篇关于如何在不与其他log4r记录器冲突的情况下在log4r中定义自己的级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆