Using log4r and rails

hey everyone. i know somebody else had started a thread on this subject,
but since no one seems to be answering that thread i thought i would
start one of my one.

i’m doing a project for a client and we need some seriously verbose
logging. it looks like log4r can do what i need, but i can’t for the
life of me figure out how to plug it into rails. everywhere talks about
how easy it is, but nowhere can i find a description of how to actually
do it.

i’m using rails 1.1.2, lighttpd 1.4.11, and i’m on a mac.

please, any help would be very much appreciated!

thanks.
-mark

Hey Mark,

I’m still on Rails 1.0 for the project that uses Log4R and although I
know there are 1 or 2 bugs in trac for rails integrating with it, most
things should be the same…
Also, I am deployed on FreeBSD under lighty so there should be no
problems with your setup.

In environment/rb I’ve got a require “logger” which is another file in
the environment dir

In logger.rb, I’ve copied a lot of the sample code off the log4r site,
but it looks pretty much like this:

[snip]

Configure logging.

We use various outputters, so require them, otherwise config chokes

require ‘log4r’
require ‘log4r’
require ‘log4r/yamlconfigurator’
require ‘log4r/outputter/fileoutputter’
require ‘log4r/outputter/datefileoutputter’
require ‘log4r/outputter/emailoutputter’

cfg = Log4r::YamlConfigurator
cfg[‘RAILS_ROOT’] = RAILS_ROOT
cfg[‘RAILS_ENV’] = RAILS_ENV

load the YAML file with this

cfg.load_yaml_file(“#{RAILS_ROOT}/config/log4r.yaml”)
RAILS_DEFAULT_LOGGER = Log4r::Logger[‘default’]
RAILS_DEFAULT_LOGGER.level = (RAILS_ENV == ‘development’ ?
Log4r::DEBUG : Log4r::INFO)

if RAILS_ENV == ‘test’
Log4r::Outputter[‘stderr’].level = Log4r::OFF
RAILS_DEFAULT_LOGGER.add( Log4r::Outputter[‘stderr_test’] )
end

if RAILS_ENV == ‘production’
Log4r::Outputter[‘standardlog’].level = Log4r::OFF
Log4r::Outputter[‘stderr’].level = Log4r::OFF
else
Log4r::Outputter[‘email’].level = Log4r::OFF
end
[/snip]

As you can see, I decided to keep the logger configuration all in one
place (out of the /environments/*.rb files). Note that this does not
include my log4r.yaml file, but it is heavily based on the
log4r_yaml.yaml file in the examples directory from the gem. My
stderr_test outputter is just like my stderr one but more verbose log
detail (using the ‘slow’ formatters) and it only outputs at ERROR not
WARN which keeps those Rake runs mostly free from error messages.

Note the line “RAILS_DEFAULT_LOGGER = Log4r::Logger[‘default’]” - this
is the line that tells Rails not to set up its own logger, with
“default” defined like this:

[snip]
loggers:
- name : default
level : DEBUG
additive : ‘false’
trace : ‘true’
outputters:
- stderr
- datefilelog
- standardlog
- email
[/snip]

I should point out that in the last if/else block that I am turning
off logging to the “production.log” file and only logging to a
DateFileOutputter (so that mu production logs look like
“dispatcher.fcgi.20060412.log”

[snip]
- type : DateFileOutputter
name : datefilelog
level : DEBUG
date_pattern: ‘%Y%m%d’
trunc : ‘false’
dirname : “#{RAILS_ROOT}/log”
formatter :
date_pattern: ‘%y%m%d %H:%M:%S’
#pattern : '%d %l: %m ’
pattern : “[%l] %d :: %m”
type : PatternFormatter
[/snip]

Hope that helps!
Dan


Dan S.
www.peoplehub.com.au
www.dansketcher.com

Dan S. wrote:

Hey Mark,

I’m still on Rails 1.0 for the project that uses Log4R and although I
know there are 1 or 2 bugs in trac for rails integrating with it, most

Hai Dan,

Seems to me you’re an log4r expert. Could you please have a look at my
humble question? It isn’t even about rails, but about log4r and ruby in
the first place.

A have the following combination of ruby code and a log4r xml config
file, and it just doesn’t want to work:

logger_tester.rb:
class LoggerTester

require 'log4r'
require 'log4r/configurator'
include Log4r

Log4r::Configurator.load_xml_file("log4r_config.xml")

@@my_log = Logger.new('LoggerTestLogger')

def do_log
  	@@my_log.debug("==>Log test")
end

end

myTester = LoggerTester.new
myTester.do_log

log4r_config.xml:
<log4r_config>


false
console

</log4r_config>

It won’t give any output. Can you see why?

Tnxs