在Rails记录器中包含参数/请求信息?

时间:2022-03-19 00:18:38

I'm trying to get some more information into my Rails logs, specifically the requested URI or current params, if available (and I appreciate that they won't always be). However I just don't seem able to. Here's what I've done so far:

我正在尝试在Rails日志中获取更多信息,特别是请求的URI或当前参数(如果可用)(我感谢他们并非总是这样)。但是我似乎无法做到。这是我到目前为止所做的:

#config/environments/production.rb
config.logger = Logger.new(config.log_path)
config.log_level = :error
config.logger.level = Logger::ERROR

#config/environment.rb
class Logger
  def format_message(level, time, progname, msg)
    "**********************************************************************\n#{level} #{time.to_s(:db)} -- #{msg}\n"
  end  
end

So I can customize the message fine, yet I don't seem to be able to access the params/request variables here. Does anyone know if this is possible, and if so how? Or if there's a better way to get this information? (Perhaps even something Redis based?)

所以我可以自定义消息,但我似乎无法在这里访问params / request变量。有谁知道这是否可能,如果是这样的话怎么样?或者,如果有更好的方法来获取此信息? (甚至可能是Redis的基础?)

Thanks loads,

谢谢你,

Dan

3 个解决方案

#1


25  

(Responding a long time after this was asked, but maybe it will help the next person.)

(在被问到这个问题很长一段时间后回复,但也许这对下一个人有帮助。)

I just did something similar.

我刚做了类似的事。

1) you need to override your logger separately from logging request-leve details. Looks like you've figured customizing your logger out. Answer is here: Rails logger format string configuration

1)您需要单独覆盖记录器以记录请求级别详细信息。看起来你已经想出了自定义你的记录器了。答案在这里:Rails记录器格式字符串配置

2) I log the request and response of all requests into my service. Note, that Rails puts a tonne of stuff into the headers, so just straight dumping the request or the headers is probably a bad idea. Also of note, my application is primarily accessed via an API. If yours is primarily a web-app, as I'm guessing most people's are, you probably don't want to inspect the response.body as it will contain your html.

2)我将所有请求的请求和响应记录到我的服务中。注意,Rails会将大量内容放入标题中,因此直接转储请求或标题可能是一个坏主意。另外值得注意的是,我的应用程序主要通过API访问。如果你的主要是一个网络应用程序,因为我猜大多数人都是,你可能不想检查response.body,因为它将包含你的HTML。

class ApplicationController < ActionController::Base 
  around_filter :global_request_logging
...
  def global_request_logging 
    http_request_header_keys = request.headers.keys.select{|header_name| header_name.match("^HTTP.*")}
    http_request_headers = request.headers.select{|header_name, header_value| http_request_header_keys.index(header_name)}
    logger.info "Received #{request.method.inspect} to #{request.url.inspect} from #{request.remote_ip.inspect}.  Processing with headers #{http_request_headers.inspect} and params #{params.inspect}"
    begin 
      yield 
    ensure 
      logger.info "Responding with #{response.status.inspect} => #{response.body.inspect}"
    end 
  end 
end

#2


3  

This should work! :) cheers.

这应该工作! :)干杯。

logger.info({:user_agent => request.user_agent, :remote_ip => request.remote_ip}.inspect)

logger.info({:user_agent => request.user_agent,:remote_ip => request.remote_ip} .inspect)

logger.info(params.inspect)

logger.info(params.inspect)

By the by.. This should be placed in your controllers action. Ex: If you place it in your create action it should also log the user_agent i.e the browser, remote_ip i.e the remote ip of the user and all the params.

by by ..这应该放在你的控制器动作中。例如:如果你把它放在你的创建动作中,它也应该记录user_agent,即浏览器,remote_ip,即用户的远程ip和所有参数。

#3


0  

you should look in the request class

你应该查看请求类

like puts request.uri.

喜欢puts request.uri。

check here for more detail http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html

点击此处查看更多详情http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html

#1


25  

(Responding a long time after this was asked, but maybe it will help the next person.)

(在被问到这个问题很长一段时间后回复,但也许这对下一个人有帮助。)

I just did something similar.

我刚做了类似的事。

1) you need to override your logger separately from logging request-leve details. Looks like you've figured customizing your logger out. Answer is here: Rails logger format string configuration

1)您需要单独覆盖记录器以记录请求级别详细信息。看起来你已经想出了自定义你的记录器了。答案在这里:Rails记录器格式字符串配置

2) I log the request and response of all requests into my service. Note, that Rails puts a tonne of stuff into the headers, so just straight dumping the request or the headers is probably a bad idea. Also of note, my application is primarily accessed via an API. If yours is primarily a web-app, as I'm guessing most people's are, you probably don't want to inspect the response.body as it will contain your html.

2)我将所有请求的请求和响应记录到我的服务中。注意,Rails会将大量内容放入标题中,因此直接转储请求或标题可能是一个坏主意。另外值得注意的是,我的应用程序主要通过API访问。如果你的主要是一个网络应用程序,因为我猜大多数人都是,你可能不想检查response.body,因为它将包含你的HTML。

class ApplicationController < ActionController::Base 
  around_filter :global_request_logging
...
  def global_request_logging 
    http_request_header_keys = request.headers.keys.select{|header_name| header_name.match("^HTTP.*")}
    http_request_headers = request.headers.select{|header_name, header_value| http_request_header_keys.index(header_name)}
    logger.info "Received #{request.method.inspect} to #{request.url.inspect} from #{request.remote_ip.inspect}.  Processing with headers #{http_request_headers.inspect} and params #{params.inspect}"
    begin 
      yield 
    ensure 
      logger.info "Responding with #{response.status.inspect} => #{response.body.inspect}"
    end 
  end 
end

#2


3  

This should work! :) cheers.

这应该工作! :)干杯。

logger.info({:user_agent => request.user_agent, :remote_ip => request.remote_ip}.inspect)

logger.info({:user_agent => request.user_agent,:remote_ip => request.remote_ip} .inspect)

logger.info(params.inspect)

logger.info(params.inspect)

By the by.. This should be placed in your controllers action. Ex: If you place it in your create action it should also log the user_agent i.e the browser, remote_ip i.e the remote ip of the user and all the params.

by by ..这应该放在你的控制器动作中。例如:如果你把它放在你的创建动作中,它也应该记录user_agent,即浏览器,remote_ip,即用户的远程ip和所有参数。

#3


0  

you should look in the request class

你应该查看请求类

like puts request.uri.

喜欢puts request.uri。

check here for more detail http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html

点击此处查看更多详情http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html