简体   繁体   English

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

[英]How do I get Rails to log my Rack::CommonLogger messages?

I am using the ruby gem rest-client with rest-client-components . 我正在使用ruby gem rest-clientrest-client-components

Rest-client-components enables request logging with Rack::CommonLogger. Rest-client-components使用Rack :: CommonLogger启用请求日志记录。

The instructions for enabling it make use of STDOUT: 启用它的说明使用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. 这在开发中工作正常,但是当我使用Apache / Passenger(mod_rails)进行生产时,在production.log中看不到来自rest-client的任何消息。 Is there a way to integrate Rack::CommonLogger with the Rails log? 有没有一种方法可以将Rack :: CommonLogger与Rails日志集成在一起? 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. 谢谢。

Here's the solution I came up with. 这是我想出的解决方案。 Thanks to @crohr for pointing me in the right direction. 感谢@crohr为我指出正确的方向。

First, create a new Logger class. 首先,创建一个新的Logger类。 Rails defaults to ActiveSupport::BufferedLogger, so we'll extend that. Rails默认为ActiveSupport :: BufferedLogger,因此我们将对其进行扩展。

# 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. 然后告诉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

Finally, tell rest-client to use your new logger. 最后,告诉rest-client使用您的新记录器。

# 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. 如果您将Rack :: Cache与rest-client-components一起使用,则不会捕获缓存消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Java日志消息保存到类似Rails日志消息的文件中? - How do I save Java log messages to a file like my Rails log messages? 如何抑制Rails的“过期片段”日志消息? - How do I suppress Rails “Expire fragment” log messages? 如何将独立的Rack应用程序安装到Ruby On Rails应用程序中? - How do I mount a standalone Rack app into a Ruby On Rails app? 如何使ActionMailer记录发送的邮件但不包括附件? - How do I get ActionMailer to log sent messages but NOT include the attachments? 如何使用rails限制对已安装的机架应用程序的访问? - How do I restrict access to a mounted rack application using rails? 如何从Rails中访问Rack环境? - How do I access the Rack environment from within Rails? 如何使用 Rails 3 / Rack 在没有 www 的情况下重定向? - How do I redirect without www using Rails 3 / Rack? 如何获取我的Rails 5日志以向我显示有关Rails 4等每项操作的详细信息? - How do I get my Rails 5 log to show me as many details about each operation like Rails 4 did? 如何记录来自Rails控制器的响应? - How do I log responses from my Rails Controllers? 如何在另一个Rails应用程序中将Rails应用程序设置为机架应用程序? - How do I set up rails apps as rack apps within another rails app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM