Forum: Ruby on Rails log4r and rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Helium E. (Guest)
on 2006-04-08 01:55
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!
Mark B. (Guest)
on 2006-04-10 02:54
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!
Fino Fontana (Guest)
on 2006-05-19 10:50
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
Fino F. (Guest)
on 2006-07-15 22:02
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:
<!--
Log Configuration
-->

<log4r_config>

  <pre_config>
    <custom_levels>DEBUG, INFO, WARN, ERROR, FATAL</custom_levels>
    <global level="ALL"/>
  </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>

2) 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


3) In environment.rb I load and run LogInitializer:

  #Initializing loggers
   require 'log_initializer'
   li = LogInitializer.new
   li.init()

4) 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


5) 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
This topic is locked and can not be replied to.