I was wondering if there are any truly unified logging systems out there that can support Rails and delayed j开发者_Python百科obs, 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.
精彩评论