# Using log4r and rails

**URL:** <https://www.ruby-forum.com/t/using-log4r-and-rails/57494>\
**Category:** Rails\
**Created:** [April 11, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/using-log4r-and-rails/57494 "2006-04-11T14:54:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markbates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@markbates](https://www.ruby-forum.com/u/markbates)\
**Post date:** [April 11, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/using-log4r-and-rails/57494/1 "2006-04-11T14:54:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markbates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@markbates](https://www.ruby-forum.com/u/markbates)\
**Post date:** [April 12, 2006, 12:50am UTC](https://www.ruby-forum.com/t/using-log4r-and-rails/57494/2 "2006-04-12T00:50:43Z")

</div>

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](http://www.dansketcher.com)

---

<div class="post-metadata">

**Author:** ![markbates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@markbates](https://www.ruby-forum.com/u/markbates)\
**Post date:** [May 24, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/using-log4r-and-rails/57494/3 "2006-05-24T23:50:57Z")

</div>

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
