tags:

views:

698

answers:

3

Hi,

Can some one post an example of using syslog outputter for log4r, I am currently using stdout but want to log to syslog.

mylog = Logger.new 'mylog'
mylog.outputters = Outputter.stdout
mylog.info "Starting up."

raj

+3  A: 

Hi,

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

Rajkumar S
+1  A: 

Thanks also to the following blog posts.

Angrez's blog: Log4r - Usage and Examples

ProgrammingStuff: Log4r

Rajkumar S
A: 

I found this very helpful, but I had to make further edits. Something tried to re-open the syslog, causing an unhandled RuntimeError. I fixed it with this axe-crazy override in environments/production.rb:

require 'rubygems'
require 'log4r'
require 'log4r/outputter/syslogoutputter'

# The outputter needs some love to avoid attempts to reopen syslog. Most of this is cargo-culted from source.
class Log4r::SyslogOutputter
  def initialize(_name, hash={})
    super(_name, hash)
    ident = (hash[:ident] or hash['ident'] or _name)
    logopt = (hash[:logopt] or hash['logopt'] or LOG_PID | LOG_CONS).to_i
    facility = (hash[:facility] or hash['facility'] or LOG_USER).to_i
    if Syslog.opened? then
      @syslog = Syslog
    else
      @syslog = Syslog.open(ident, logopt, facility)
    end
  end
end

RAILS_DEFAULT_LOGGER = Log4r::Logger.new 'mylog'
config.logger = RAILS_DEFAULT_LOGGER
config.logger.outputters = Log4r::SyslogOutputter.new("f1", :ident=>"RoR")
config.logger.info "Starting up."

There's probably a much prettier way of doing it, but this would seem to do it for me.

Anders Eurenius