[英]Logging in Ruby/Rails
I have the following code: 我有以下代码:
def get_request(resource)
request = Typhoeus::Request.new("#{@BASE_URL}#{resource}",
userpwd: "#{@USER}:#{@PWD}",
headers: { 'Content-Type' => "application/x-www-form-urlencoded"})
response = request.run.body
puts response
end
Instead of puts response
, I want to log the entire response
. 我不想记录
puts response
,而是要记录整个response
。 What's the best/most efficient way to do that? 最好/最有效的方法是什么? Regardless of what
response
is, it should be logged. 无论
response
是什么,都应该记录下来。 I feel like opening a file, writing to it and closing it every time this method is used would be pretty inefficient. 我觉得每次使用此方法时,打开文件,写入文件并关闭它都是非常低效的。 Is there a better way?
有没有更好的办法?
If you are using Rails
as the tag supposes, you can use 如果您使用的是
Rails
的标签,则可以使用
Rails.logger
to use the default Rails
logger. 使用默认的
Rails
记录器。 Here's an example. 这是一个例子。
Rails.logger.info response.body
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.