在Rails应用中记录RestClient [英] Logging RestClient in a Rails app

查看:94
本文介绍了在Rails应用中记录RestClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 RestClient 调试我的Rails应用发出的请求. RestClient文档说:

I'd like to debug the request my Rails app makes with RestClient. The RestClient docs say:

要启用日志记录,您可以

To enable logging you can

使用ruby Logger设置RestClient.log 或设置环境变量以避免修改代码(在这种情况下,您可以使用文件名"stdout"或"stderr"):

set RestClient.log with a ruby Logger or set an environment variable to avoid modifying the code (in this case you can use a file name, "stdout" or "stderr"):

$ RESTCLIENT_LOG =标准输出路径/到/我/程序 要么生成这样的日志:

$ RESTCLIENT_LOG=stdout path/to/my/program Either produces logs like this:

RestClient.get" http://some/resource "

RestClient.get "http://some/resource"

=> 200 OK | text/html 250字节

=> 200 OK | text/html 250 bytes

RestClient.put" http://some/resource ",有效载荷"

RestClient.put "http://some/resource", "payload"

=> 401未经授权| application/xml 340字节

=> 401 Unauthorized | application/xml 340 bytes

请注意,这些日志是有效的Ruby,因此您可以将其粘贴到restclient shell或>脚本中,以重播rest调用的顺序.

Note that these logs are valid Ruby, so you can paste them into the restclient shell or a >script to replay your sequence of rest calls.

如何将这些日志包含在我的Rails应用日志文件夹中?

How do I do get these logs included in my Rails apps log folder?

推荐答案

来自: https://gist. github.com/jeremy/1383337

require 'restclient'

# RestClient logs using << which isn't supported by the Rails logger,
# so wrap it up with a little proxy object.
RestClient.log =
  Object.new.tap do |proxy|
    def proxy.<<(message)
      Rails.logger.info message
    end
  end

这篇关于在Rails应用中记录RestClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆