# Log4r and rails

**URL:** <https://www.ruby-forum.com/t/log4r-and-rails/57216>\
**Category:** Rails\
**Created:** [April 7, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/log4r-and-rails/57216 "2006-04-07T23:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![helium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/2acd7d/32.png) [@helium](https://www.ruby-forum.com/u/helium)\
**Post date:** [April 7, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/log4r-and-rails/57216/1 "2006-04-07T23:55:47Z")

</div>

Hey there folks. I would really like to use log4r as my default logger  
in Rails, but I’m having a hard time getting it to work. I’m not even  
sure I’m plugging it in in the right place. I’m using log4r 1.0.5 and  
rails 1.1.1.

I would love it if someone could either point me to a good tutorial on  
how to use log4r with rails, or at least point me in the right direction  
as far as how to configure/set it up.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![helium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/2acd7d/32.png) [@helium](https://www.ruby-forum.com/u/helium)\
**Post date:** [April 10, 2006, 12:54am UTC](https://www.ruby-forum.com/t/log4r-and-rails/57216/2 "2006-04-10T00:54:52Z")

</div>

me too!! sorry, i don’t have the answer to this question, but today i’ve  
been trying to figure out how to use for a client’s project, and i can’t  
figure it out either! i thought i was the idiot!

i’m also using 1.0.5 of log4r, as well as rails 1.1.2, and lighttpd  
1.4.11.

does anyone out there know how to configure log4r with rails??

thanks peeps.  
-mark

Helium Exchange wrote:

> Hey there folks. I would really like to use log4r as my default logger  
> in Rails, but I’m having a hard time getting it to work. I’m not even  
> sure I’m plugging it in in the right place. I’m using log4r 1.0.5 and  
> rails 1.1.1.
> 
> I would love it if someone could either point me to a good tutorial on  
> how to use log4r with rails, or at least point me in the right direction  
> as far as how to configure/set it up.
> 
> Thanks a lot!

---

<div class="post-metadata">

**Author:** ![helium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/2acd7d/32.png) [@helium](https://www.ruby-forum.com/u/helium)\
**Post date:** [July 15, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/log4r-and-rails/57216/3 "2006-07-15T20:02:11Z")

</div>

Fino Fontana wrote:

> Any progress on this subject? Also an example of a simple xml  
> configuration would be welcome. The log4r manual and xml example a  
> rather poor (with due respect to the maker of log4r himself, as I think  
> log4r is very promising)
> 
> Thanks,  
> Fino

This is how I got it working:

Assuming Rails and Log4R installed:

1. In the config directory I have a configuration file called  
log4r\_config.xml with the following contents:

\<log4r\_config\>

\<pre\_config\>  
\<custom\_levels\>DEBUG, INFO, WARN, ERROR, FATAL\</custom\_levels\>  
  
\</pre\_config\>

```
<!-- Outputters -->
<outputter name="console" type="StdoutOutputter" level="DEBUG" >
	<formatter type="Log4r::PatternFormatter">
		<pattern>=>[%5l %d] %C: %M [%t]</pattern>
	</formatter>
</outputter>

<outputter name="file_outputter" type="FileOutputter">
	<filename>log/working_time.log</filename>
	<formatter type="Log4r::PatternFormatter">
		<pattern>=>[%5l %d] %C: %M [%t]</pattern>
	</formatter>
</outputter>

<!-- Loggers -->
<logger name="WorkingDayControllerLogger"
            level="ALL" additive="false" trace="true">
	<outputter>console</outputter>
	<outputter>file_outputter</outputter>
</logger>

<logger name="WorkingTimeControllerLogger"
            level="ALL" additive="false" trace="true">
	<outputter>console</outputter>
	<outputter>file_outputter</outputter>
</logger>

```

\</log4r\_config\>

1. In the helper directory I have a class LogInitializer, which  
initializes the Log4r system and loads config/log4r\_config.xml:

class LogInitializer

require ‘log4r’  
require ‘log4r/configurator’  
include Log4r

def init()  
puts “==\>LogInitializer: Starting initialization of loggers”  
Configurator[‘logpath’] = ‘./config’ # was ‘./logs’  
puts ("==\>LogInitializer: Loading xml config")  
Configurator.load\_xml\_file(‘config/log4r\_config.xml’)  
puts ("==\>LogInitializer: Xml config Loaded")  
puts “==\>LogInitializer: Loggers initialized”  
end

end

1. In environment.rb I load and run LogInitializer:

#Initializing loggers  
require ‘log\_initializer’  
li = LogInitializer.new  
li.init()

1. In the source of a class I define a logger as follows:

class WorkingDayController \< ApplicationController

# Logging

require ‘log4r’  
require ‘log4r/configurator’  
include Log4r  
@@my\_log = Logger[“WorkingDayControllerLogger”]  
…  
end

1. In the source of a class I use a logger as follows:  
def index  
@@my\_log.debug(“in index”)  
list  
render :action =\> ‘list’  
end

Hope this will help.

:regards =\> Fino

---

<div class="post-metadata">

**Author:** ![helium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/2acd7d/32.png) [@helium](https://www.ruby-forum.com/u/helium)\
**Post date:** [May 19, 2006, 8:50am UTC](https://www.ruby-forum.com/t/log4r-and-rails/57216/4 "2006-05-19T08:50:50Z")

</div>

Mark B. wrote:

> me too!! sorry, i don’t have the answer to this question, but today i’ve  
> been trying to figure out how to use for a client’s project, and i can’t  
> figure it out either! i thought i was the idiot!
> 
> i’m also using 1.0.5 of log4r, as well as rails 1.1.2, and lighttpd  
> 1.4.11.
> 
> does anyone out there know how to configure log4r with rails??
> 
> thanks peeps.  
> -mark
> 
> Helium Exchange wrote:
> 
> > Hey there folks. I would really like to use log4r as my default logger  
> > in Rails, but I’m having a hard time getting it to work. I’m not even  
> > sure I’m plugging it in in the right place. I’m using log4r 1.0.5 and  
> > rails 1.1.1.
> > 
> > I would love it if someone could either point me to a good tutorial on  
> > how to use log4r with rails, or at least point me in the right direction  
> > as far as how to configure/set it up.
> > 
> > Thanks a lot!

Any progress on this subject? Also an example of a simple xml  
configuration would be welcome. The log4r manual and xml example a  
rather poor (with due respect to the maker of log4r himself, as I think  
log4r is very promising)

Thanks,  
Fino
