¿Existe un sistema de registro unificado para Ruby?
-
10-10-2019 - |
Pregunta
Me preguntaba si hay sistemas de registro verdaderamente unificadas por ahí que puede Carriles soporte y Empleo retraso, y son relativamente fáciles de instalar.
Quiero ser capaz de conectarse al mismo servidor / archivo para cualquier contexto de ejecución en mi solicitud (rieles, los trabajos de retraso, etc.), incluso si no estoy actualmente en un contexto rieles.
Amor de los rieles registrador, pero no puede conectarse a él, mientras que en un trabajo Resque. ¿Algunas ideas?
Solución
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.
Otros consejos
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.