views:

146

answers:

2

Hello,

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,

+1  A: 

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 
Mladen Jablanović
Hi, thanks for your answer, however this example does not seem to work properly in our environnement. I get a 'decode_yaml': undefined method `each' for nil:NilClass (NoMethodError)' when the yaml file is parsed. Parsing the file or a hash gives quite the same error ... Are you using a 1.1.17 version of log4r ?
devlearn
I'm using 1.0.6 in production. I will try it out a bit later and send a working example.
Mladen Jablanović
I pasted the example straight from irb.
Mladen Jablanović
Using 1.1.7, the code above works for me *if* I add `include Log4r` right after the second `require`. See [this gist](http://gist.github.com/419328). Other than that, works beautifully, thank you for providing this great example!
Noah Sussman
A: 

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.

devlearn
The sourceforge link you mention is for Log4r 1.0.5. The OP is using 1.1.7.To see the API documentation for Log4r, say: `gem server` Then go to http://localhost:8808 and you can read the API docs for the installed versions of all your gems.
Noah Sussman