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

时间:2021-01-18 03:59:20

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

我正在使用ruby gem rest-client和rest-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. 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.

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

Thanks.

1 个解决方案

#1


3  

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. Rails defaults to ActiveSupport::BufferedLogger, so we'll extend that.

首先,创建一个新的Logger类。 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一起使用,则不会捕获缓存消息。

#1


3  

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. Rails defaults to ActiveSupport::BufferedLogger, so we'll extend that.

首先,创建一个新的Logger类。 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一起使用,则不会捕获缓存消息。