Skip to content

Log4r Yaml Configuration

Lauren Voswinkel edited this page May 15, 2014 · 2 revisions

It's easy

Take a deep breath, no hyper-ventilation now.

I'm going to do an in-depth review of how to do this in Rails 3.0+ for others, all I can say is, try your best to infer this stuff, or hit me up on here with a message about how to do it for your preferred server/environment and I'll update as needed.

In Rails 3.0+

First you need the actual configuration file, here's an example of one, let's put it at #{RAILS_ROOT}/config/log4r.yml:

 log4r_config:
   loggers:
   - name: "foo"
     outputters:
     - foo
   outputters:
   - name: foo
     type: StdoutOutputter
     default: true
     formatter:
       type: PatternFormatter
       pattern: '%d [%l] - Foo'

Above is an example of how you could configure an outputter. Sadly, YOU MUST INCLUDE AT LEAST ONE DEFINED LOGGER WITH THE CONFIGURATION, if you do not, it will think it's an orphan configuration file and weep softly to itself.

With the configuration in hand, we go boldly forth to our config/boot.rb file (or config/environment.rb or config/environments/#{RAILS_ENV}.rb etc.) Once there, plop the following lines where you please:

 require 'log4r'
 require 'log4r/yamlconfigurator'
 require 'easy_log4r'
 Log4r::YamlConfigurator.load_yaml_file(File.expand_path('../log4r.yml', __FILE__))

The require 'easy_log4r' isn't necessary for this... but hey, that's why you're here in the first place, right?

Wrap-up

Anyways, with that done, you're good to go. To understand what all the options are for your log4r configuration, I recommend hitting up the following links:

And these: (Special attention to the attributes, they're effectively a 1-to-1 equivalence)

Anything else you might need can be found in the different types of Outputters and Formatters (we do the Logger stuff for you, aren't we nice?)

Other Info

If you otherwise just want to know more about Log4r, I suggest you head here.

Clone this wiki locally