Properly Using Log4R in Ruby Application

Properly using Log4r in Ruby Application

Hmm, any reason why you don't instantiate Log4r::Logger class at the beginning of your script and pass the instance around? You don't even have to pass it around, you can always get it by name from Logger class:

run.rb:

require 'log4r'
require 'class_a'

logger = Log4r::Logger.new('test')
logger.outputters << Log4r::Outputter.stdout
logger.outputters << Log4r::FileOutputter.new('logtest', :filename => 'logtest.log')
logger.info('started script')
a = A.new
a.do_something
logger.info('finishing')

class_a.rb:

class A
def do_something
logger = Log4r::Logger['test']
logger.info('in do_something')
puts 'hi!'
end
end

and when you run run.rb you get:

$ ruby run.rb 
INFO test: started script
INFO test: in do_something
hi!
INFO test: finishing

and a log file named logtest.log on disk.

Getting error using log4r with delayed job

( see blog.mmlac.com for original comment on this issue. )

The issue is that DelayedJob expects the logger to be Rails::Logger and makes calls to it that log4r does not support.

In this article the logger variable is overwritten to use Log4r::Logger, which does not support the .add call as intended. Unlike in Java there are no packages to determine which logger class to use or slf4j that unites different loggers to a standard interface.

There is no simple solution to this issue. On the one hand you can overwrite the affected parts of delayedJob. On the other hand you can prevent loading the log4r when delayedJob is running rails, i.e. by using a custom environment:

  • development
  • production
  • delayedJob

This is also not guaranteed to work everywhere (anything that checks the rails env == “production” will have issues).

Another way is to use environment variables and check those in the application.rb:

if (ENV["log4rlogger"] == "true") config.logger = Log4r::Logger["rails"]

syslog output for log4r example

Kind of lame answering my own question, but I found answer to this and adding it for later searches.

For some reason I need to require log4r/outputter/syslogoutputter explicitly other wise SyslogOutputter would cause "uninitialized constant SyslogOutputter (NameError)" error. Other outputters do not seem to have this problem.

require 'rubygems'
require 'log4r'
require 'log4r/outputter/syslogoutputter'
mylog = Logger.new 'mylog'
mylog.outputters = SyslogOutputter.new("f1", :ident => "myscript")
mylog.info "Starting up."

raj

Log4r : logger inheritance, yaml configuration, alternatives?

I agree that the log4r documentation is pretty poor. We are using it though and it serves us pretty well, in let's-say an enterprisey app.

We are not using logger inheritance so I can't help you with it, and also I don't know about any alternative software, but:

Here's the code we use to read YAML configuration (in fact, I think we pass it as already loaded into a Hash), it also supports variable substitution:

require 'log4r'
require 'log4r/yamlconfigurator'

y = "log4r_config:

# define all loggers ...
loggers:
- name : production
level : INFO
trace : 'false'
outputters:
- stdout

# define all outputters (incl. formatters)
outputters:
- type : StdoutOutputter
name : stdout
formatter:
date_pattern: '%Y-%m-%d %H:%M:%S'
pattern : '%d %l: #\{TEST\} %m '
type : PatternFormatter"

h = YAML.load y
log_cfg = YamlConfigurator
log_cfg['TEST'] = 'foobar'
log_cfg.decode_yaml h['log4r_config']
@log = Logger['production']
@log.info 'test'
#=>2010-05-20 14:36:32 INFO: foobar test

Ruby on Rails - Using LogServer for Remote Logging in log4r using ROMP

From this link: http://www.ruby-forum.com/topic/103958

After downloading and extracting the ROMP tarball ...

cd romp-0.2
ruby extconf.rb
make

This will compile the ROMP C extension. Now you need to install it into your ruby directory. On my Windows/Cygwin bastard box this is found in ...

/lib/ruby/site_ruby/1.8

The ROMP .rb files go directly into this directory, and the compiled .so goes into the

/lib/ruby/site_ruby/1.8/i386-cygwin

directory.

Please note that I have not tried this installation myself, but I hope
you get the general idea

rails 4 log4r server.rb:78:in `start': undefined method `formatter'

Looking at this post [1] you might have to import the FileOutputter as well. Not really sure.

I think it's an import issue when the other server works fine.
Just try to import everything you might possibly use from Log4r and make sure you don't call in [1] mentioned functions directly on the logger somewhere else.

If that does not work, try setting up a simple logger programmatically, then move that logger to the .yml and then expand it back to where it was before.

Hope this helps you, let me know if you need more help

[1] Undefined Method Formatter for Log4r in RAILS 4.0

Log4r - output is unordered

ruby is single-threaded, so I assume you are running your rails app on a server like unicorn?

The only solution I can think of is timestamps (with micro seconds) and not using the multi-line Rails log output which is not suitable for anything but single requests in dev environments. But even this does not guarantee "ordered" logs because they can be written when the request was served or at a random time afterwards when the buffer was flushed again.

This is an in-depth tutorial on how to use Log4r that also shows how to get custom timestamps into your logs and have all the important information on a single line. This prevents logs from other requests writing inside your main request and you can have a timestamp resolution of microseconds and order afterwards manually. However the order should not matter if you are running concurrent app-servers because the order is not guaranteed there anyway.



Related Topics



Leave a reply



Submit