문제

I was wondering if there are any truly unified logging systems out there that can support Rails and delayed jobs, and are relatively easy to set up.

I want to be able to log to the same server/file for any execution context in my application (Rails, delayed jobs, etc), even if i'm not currently in a Rails context.

Love the Rails logger, but I can't log to it while in a Resque job. Any ideas?

도움이 되었습니까?

해결책

Do you mean a file-logger, similar to syslog?

Ruby's got both Logger and Syslog.

Logger can do log rolling, handles severity levels, and is used in a lot of Ruby modules for logging. You can define the name of the file to log to, or use STDOUT/STDERR or an IO stream.

The docs for syslog are pretty barebones, but you can get info by browsing its source code, or reading the Ruby Syslog README.

다른 팁

I have to log things that are happening in a gem which runs by a resque job. To log what is going on to Rails database I do the following:

#in gem:
class Foo
  def self.logger
    @@logger ||= Logger.new(nil)
  end

  def self.logger=(logger)
    @@logger = logger
  end

  def self.logger_reset
    self.logger = Logger.new(nil)
  end

  def self.logger_write(obj_id, message, method = :info)
    self.logger.send(method, "|%s|%s|" % [obj_id, message])
  end
end

#in rails in initializers
Foo.logger = MyRailsLogger.new

#in in rails in lib or in model if it uses ActiveRecord
class MyRailsLogger
  def info
    ...
  end

  ...
end

This way I can log things which are happening in different process and also filter logs by object_id of the Foo instance, so only relevant data gets logged.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top