如何让 Rails 记录我的 Rack::CommonLogger 消息?

发布于 2024-12-22 11:28:58 字数 653 浏览 3 评论 0原文

我正在使用 ruby​​ gem rest-clientrest-client com/crohr/rest-client-components" rel="nofollow">rest-client-components。

Rest-client-components 支持使用 Rack::CommonLogger 进行请求日志记录。

启用它的说明使用 STDOUT:

require 'restclient/components'
RestClient.enable Rack::CommonLogger, STDOUT

这在开发中工作得很好,但是当我使用 Apache/Passenger (mod_rails) 进行生产时,我在 production.log 中看不到来自rest-client 的任何消息。有没有办法将 Rack::CommonLogger 与 Rails 日志集成?或者至少将其写入文件?前者更有用,因为很容易看到上下文,但后者总比没有好。

谢谢。

I am using the ruby gem rest-client with rest-client-components.

Rest-client-components enables request logging with Rack::CommonLogger.

The instructions for enabling it make use of STDOUT:

require 'restclient/components'
RestClient.enable Rack::CommonLogger, STDOUT

This works fine in development, but when I'm in production with Apache/Passenger (mod_rails), I don't see any messages from rest-client in production.log. Is there a way to integrate Rack::CommonLogger with the Rails log? Or at least to write it to a file? The former is more useful because it's easy to see the context, but the latter is better than nothing.

Thanks.

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

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

发布评论

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

评论(1

无敌元气妹 2024-12-29 11:28:58

这是我想出的解决方案。
感谢@crohr 为我指明了正确的方向。

首先,创建一个新的 Logger 类。 Rails 默认为 ActiveSupport::BufferedLogger,因此我们将对其进行扩展。

# lib/rest_client_logger.rb
class RestClientLogger < ActiveSupport::BufferedLogger
  def write(msg)
    add(INFO, msg)
  end
end

然后告诉 Rails 使用你的新记录器。

# application.rb
log_file = File.open("log/#{Rails.env}.log", 'a')
log_file.sync = true  # turn on auto-flushing at the file level so we get complete messages
config.logger = RestClientLogger.new(log_file)
config.logger.auto_flushing = !Rails.env.production?  # turn off auto-flushing at the logger level in production for better performance

最后,告诉rest-client使用您的新记录器。

# config/initializers/rest_client.rb
RestClient.enable Rack::CommonLogger, Rails.logger

限制:

如果您将 Rack::Cache 与 rest-client-components 一起使用,则不会捕获缓存消息。

Here's the solution I came up with.
Thanks to @crohr for pointing me in the right direction.

First, create a new Logger class. Rails defaults to ActiveSupport::BufferedLogger, so we'll extend that.

# lib/rest_client_logger.rb
class RestClientLogger < ActiveSupport::BufferedLogger
  def write(msg)
    add(INFO, msg)
  end
end

Then tell Rails to use your new logger.

# application.rb
log_file = File.open("log/#{Rails.env}.log", 'a')
log_file.sync = true  # turn on auto-flushing at the file level so we get complete messages
config.logger = RestClientLogger.new(log_file)
config.logger.auto_flushing = !Rails.env.production?  # turn off auto-flushing at the logger level in production for better performance

Finally, tell rest-client to use your new logger.

# config/initializers/rest_client.rb
RestClient.enable Rack::CommonLogger, Rails.logger

Limitations:

If you're using Rack::Cache with rest-client-components, this doesn't capture the cache messages.

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