Ruby 有统一的日志系统吗?

发布于 2024-10-02 20:00:33 字数 172 浏览 0 评论 0 原文

我想知道是否有真正统一的日志系统可以支持 Rails 和延迟作业,并且相对容易设置。

我希望能够为我的应用程序中的任何执行上下文(Rails、延迟作业等)登录到同一服务器/文件,即使我当前不在 Rails 上下文中。

喜欢 Rails 记录器,但我在 Resque 工作时无法登录它。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-10-09 20:00:33

您是指类似于系统日志的文件记录器吗?

Ruby 同时具有 LoggerSyslog

Logger 可以进行日志滚动、处理严重性级别,并在许多 Ruby 模块中用于日志记录。您可以定义要记录的文件的名称,或使用 STDOUT/STDERR 或 IO 流。

syslog 的文档非常简单,但是您可以通过浏览其源代码或阅读 Ruby Syslog 自述文件

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.

神经大条 2024-10-09 20:00:33

我必须记录由 resque 作业运行的 gem 中发生的事情。为了记录 Rails 数据库中发生的情况,我执行以下操作:

#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

这样我可以记录不同进程中发生的情况,还可以按 Foo 实例的 object_id 过滤日志,因此只记录相关数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文