Log4r:记录器继承、yaml 配置、替代方案?

发布于 2024-09-01 14:01:48 字数 1502 浏览 10 评论 0原文

我对 ruby​​ 环境还很陌生,我一直在寻找一个不错的日志框架来在我的 ruby​​ 和 Rails 应用程序中使用它。

在我之前的经验中,我已经成功地使用了 log4j 和 log4p(perl 端口),并期望与 log4r 具有相同水平的可用性(和成熟度)。

然而我必须说,log4r 框架中有很多东西根本不清楚。

1 记录器继承

记录器继承似乎根本不受管理!

如果我声明一个名为“myapp”的记录器,然后尝试获取名称为“myapp::engine”的记录器,则查找将以 NameError 结束。

我希望框架根据命名方案返回根记录器并使用“myapp”记录器。

Q1:当然,我可以解决这个问题,并使用查找方法自己管理名称,但是有没有一种更干净的方法来做到这一点,而无需任何额外的编码?

2 YAML 配置

第二个让我困惑的是 yaml 配置。在 log4r 站点上,实际上没有关于该系统的信息,文档链接到缺失的页面,因此我能找到的所有信息都包含在 gem 的示例目录中。

我对 yaml 配置必须包含 pre_config 部分以及我需要定义自己的级别这一事实感到非常困惑。

如果我删除 pre_config 部分,或将所有“自定义”级别替换为标准级别( debug、info、warn、 fatal ),则该示例将引发以下错误:

log4r/yamlconfigurator.rb:68:in `decode_yaml': Log level must be in 0..7 (ArgumentError)

因此,似乎无法使用简单的文件,其中我们只声明框架的记录器和附加器。

Q2:我确实认为我错过了一些东西,这一定是提供简单 yaml conf 文件的一种方式。您有此类用法的示例吗?

3 XML 文件中的变量替换

Q3 :Yaml 配置系统似乎提供了这样的功能,但是我无法在 XML 文件中找到类似的功能。有什么想法吗?

4 个替代方案?

我必须说,与 log4j 和其他 log4j 端口相比,我对 log4r 的功能级别和成熟度感到非常失望。

我在使用其他语言记录 API 时遇到了这个框架,并发现自己正在尝试各种方法,只是为了使“基本事物”在“现实世界的应用程序”中运行。

我的意思是一个由多个 gem、控制台/脚本应用程序和一个 Rails Web 前端组成的复杂应用程序,其中配置必须相互化,并且我们大量使用命名空间和继承。

我进行了多次搜索,以找到更合适或更成熟的东西,但没有找到类似的东西。

Q4:你们知道可以在企业级应用程序中使用 log4r 框架的任何(严肃的)替代方案吗?

感谢阅读所有这些!

我真的很感激任何指点,

亲切的问候,

I'm pretty new to ruby environments and I was looking for a nice logging framework to use it my ruby and rails applications.

In my previous experiences I have successfully used log4j and log4p (the perl port) and was expecting the same level of usability (and maturity) with log4r.

However I must say that there are a number of things that are not clear at all in the log4r framework.

1 Logger Inheritance

The logger inheritance does not seem to be managed at all !

If I declare a logger named 'myapp' and then try to get a logger name 'myapp::engine', the lookup will end with a NameError.

I would expect that the framework returns the root logger according to the naming scheme and to use the 'myapp' logger.

Q1 : Of course I can work around this and manage the names by myself with a lookup method, however is there a cleaner way to do this without any extra coding ?

2 YAML configuration

Second thing that confuses me is the yaml configuration. On the log4r site there are literally no information about this system, the doc links forward to missing pages, so all the info I can find about is contained in the examples directory of the gem.

I was pretty confused with the fact that the yaml configuration must contain the pre_config section, and that I need to define my own levels.

If I remove the pre_config secion, or replace all the “custom” levels by the standard ones ( debug, info, warn, fatal ) , the example will throw the following error :

log4r/yamlconfigurator.rb:68:in `decode_yaml': Log level must be in 0..7 (ArgumentError)

So there seems to be no way of using a simple file where we only declare the loggers and appenders for the framework.

Q2 : I realy think that I missed something and that must be a way of providing a simple yaml conf file. Do you have any examples of such an usage ?

3 Variables substitution in XML file

Q3 : The Yaml configuration system seems to provide such a feature however I was unable to find a similar feature with XML files. Any ideas ?

4 Alternatives ?

I must say that I'm very disappointed by the feature level and the maturity of log4r compared to the log4j and other log4j ports.

I run into this framework with a solid background of logging APIs in other languages and find myself working around in all kinds just to make 'basic things' running in a “real world application”.

By that I mean a complex application composed of several gems, console/scripting apps, and a rails web front end where the configuration must be mutualized and where we make intensive usage of namespaces and inheritance.

I've run several searches in order to find something more suitable or mature, but did not find anything similar.

Q4 : Do you guys know any (serious) alternatives to log4r framework that could be used in a enterprise class app ?

Thanks reading all of this !

I'd really appreciate any pointers,

Kind Regards,

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

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

发布评论

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

评论(2

我恋#小黄人 2024-09-08 14:01:48

我同意 log4r 文档相当糟糕。不过,我们正在使用它,并且它对我们的服务非常好,比如说一个企业应用程序。

我们没有使用记录器继承,所以我无法帮助您,而且我也不知道任何替代软件,但是:

这是我们用来读取 YAML 配置的代码(事实上,我认为我们已经将其传递了加载到哈希中),它还支持变量替换:

require 'log4r'
require 'log4r/yamlconfigurator'

y = "log4r_config:

  # define all loggers ...
  loggers:
    - name      : production
      level     : INFO
      trace     : 'false'
      outputters:
        - stdout

  # define all outputters (incl. formatters)      
  outputters:
    - type     : StdoutOutputter
      name     : stdout
      formatter:
        date_pattern: '%Y-%m-%d %H:%M:%S'
        pattern     : '%d %l: #\{TEST\} %m '
        type        : PatternFormatter"

h = YAML.load y
log_cfg = YamlConfigurator
log_cfg['TEST'] = 'foobar'
log_cfg.decode_yaml h['log4r_config']
@log = Logger['production']
@log.info 'test'
#=>2010-05-20 14:36:32 INFO: foobar test 

I agree that the log4r documentation is pretty poor. We are using it though and it serves us pretty well, in let's-say an enterprisey app.

We are not using logger inheritance so I can't help you with it, and also I don't know about any alternative software, but:

Here's the code we use to read YAML configuration (in fact, I think we pass it as already loaded into a Hash), it also supports variable substitution:

require 'log4r'
require 'log4r/yamlconfigurator'

y = "log4r_config:

  # define all loggers ...
  loggers:
    - name      : production
      level     : INFO
      trace     : 'false'
      outputters:
        - stdout

  # define all outputters (incl. formatters)      
  outputters:
    - type     : StdoutOutputter
      name     : stdout
      formatter:
        date_pattern: '%Y-%m-%d %H:%M:%S'
        pattern     : '%d %l: #\{TEST\} %m '
        type        : PatternFormatter"

h = YAML.load y
log_cfg = YamlConfigurator
log_cfg['TEST'] = 'foobar'
log_cfg.decode_yaml h['log4r_config']
@log = Logger['production']
@log.info 'test'
#=>2010-05-20 14:36:32 INFO: foobar test 
面如桃花 2024-09-08 14:01:48

由于我仍在与 yaml 配置“斗争”,所以我在 XML 配置的代码中进行了一些挖掘,并找到了关于参数替换的Q3的答案。

实际上它的工作方式与 yaml 的工作方式非常相似,您所需要的只是在 xml 文件中使用 #{VARNAME} 引用参数:

<filename>#{logdir}/processing.log</filename>

并在读取 xml 文件之前在配置器中设置它们:

Log4r::Configurator['logdir']=log_dir_param
...
Log4r::Configurator.load_xml_file(conf_file_xml)

另外,当我提到log4r 文档确实状况不佳(很多 404 错误)我正在谈论 ruby​​forge 上可用的文档...

我最终通过查看 sourceforge 项目并在 < 上找到了一个“不错”(以 ruby​​ 方式)文档a href="http://log4r.sourceforge.net/rdoc" rel="nofollow noreferrer">http://log4r.sourceforge.net/rdoc。

As I'm still 'fighting' with the yaml configuration I digged a little bit in the code of the XML configuration and found the answer for Q3 concenrning the param subsitution.

Actually it works in a very similar way to the yaml stuff, all you need is to reference the params with #{VARNAME} in the xml file:

<filename>#{logdir}/processing.log</filename>

and to set them in the configurator before reading the xml file :

Log4r::Configurator['logdir']=log_dir_param
...
Log4r::Configurator.load_xml_file(conf_file_xml)

Also when I mentionned that the log4r documentation is realy in a bad shape ( lots of 404 error) I was talking about the doc available on rubyforge ...

I finaly ended by looking at the sourceforge project and found a 'nice' (in a ruby way) doc on http://log4r.sourceforge.net/rdoc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文