Ruby-on-rails 如何在 Rails 中查看整个 HTTP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2850256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:40:58  来源:igfitidea点击:

How do I see the whole HTTP request in Rails

ruby-on-railslogging

提问by akafazov

I have a Rails application but after some time of development/debugging I realized that it would be very helpful to be able to see the whole HTTP request in the logfiles - log/development.log, not just the parameters.

我有一个 Rails 应用程序,但经过一段时间的开发/调试后,我意识到能够在日志文件中查看整个 HTTP 请求非常有帮助 - log/development.log,而不仅仅是参数。

I also want to have a separate logfile based on user, not session.

我还想有一个基于用户而不是会话的单独日志文件。

Any ideas will be appreciated!

任何想法将不胜感激!

回答by microspino

You can rapidly see the request.env in a view via:

您可以通过以下方式在视图中快速查看 request.env:

  • VIEW: <%= request.env.inspect %>
  • 视图:<%= request.env.inspect %>

If instead you want to log it in development log, from your controller:

如果您想将其记录在开发日志中,请从您的控制器:

  • CONTROLLER: Rails.logger.info(request.env)
  • 控制器:Rails.logger.info(request.env)

Here you can see a referencefor the Request object.

在这里您可以看到Request 对象的引用

Rails automatically sets up logging to a file in the log/ directory using Logger from the Ruby Standard Library. The logfile will be named corresponding to your environment, e.g. log/development.log.

Rails 使用 Ruby 标准库中的 Logger 自动设置日志记录到 log/ 目录中的文件。日志文件将根据您的环境命名,例如 log/development.log。

To log a message from either a controller or a model, access the Rails logger instance with the logger method:

要从控制器或模型记录消息,请使用 logger 方法访问 Rails 记录器实例:

class YourController < ActionController::Base
  def index
    logger.info request.env
  end
end

About the user, what are you using to authenticate It?

关于用户,你用什么来认证它?

回答by AlexChaffee

That logger.info request.envcode works fine in a Rails controller, but to see a more original version of that, or if you're using Grape or some other mounted app, you have to intercept the request on its way through the rack middleware chain...

logger.info request.env代码在 Rails 控制器中运行良好,但要查看更原始的版本,或者如果您使用 Grape 或其他一些已安装的应用程序,则必须在通过机架中间件链的过程中拦截请求...

Put this code in your lib directory (or at the bottom of application.rb):

将此代码放在您的 lib 目录中(或放在 的底部application.rb):

require 'pp'
class Loggo
  def initialize(app)
    @app = app
  end
  def call(env)
    pp env
    @app.call(env)
  end
end

then in with the other configs in application.rb:

然后与其他configs in application.rb

config.middleware.use "Loggo"

回答by yfeldblum

You can use rack middleware to log the requests as the middleware sees them (as parsed by the http-server and as transformed by preceding middleware). You can also configure your http-server to log full requests, if your http-server supports such a feature.

您可以使用机架中间件记录中间件看到的请求(由 http-server 解析并由前面的中间件转换)。如果您的 http-server 支持这样的功能,您还可以配置您的 http-server 以记录完整请求。

The http-server (web-server) is responsible for receiving http requests, parsing them, and transmitting data structures to the application-server (e.g., a rack application). The application-server does not see the original request, but sees what the http-server sends its way.

http-server(web-server)负责接收http请求,解析它们,并将数据结构传送到application-server(例如,一个rack应用)。应用程序服务器看不到原始请求,但可以看到 http 服务器发送的内容。

回答by Stefan Majewsky

I've initially used the code snippet by @AlexChaffee, but I've since switched to using mitmproxy, a specialized HTTP proxy that records the requests and responses passing through it.

我最初使用了@AlexChaffee 的代码片段,但后来我改用了mitmproxy,这是一个专门的 HTTP 代理,用于记录通过它的请求和响应。

This is obviously only helpful for development scenarios when you control the applications making the requests. You might be able to achieve similar results with a reverse proxy for production applications (the advantage being that you don't have to touch the Rails application itself for this), but I haven't looked into this.

当您控制发出请求的应用程序时,这显然仅对开发场景有帮助。您可能能够使用生产应用程序的反向代理获得类似的结果(优点是您不必为此接触 Rails 应用程序本身),但我还没有研究过这一点。