0

使用REST 客户端时,我可以使用以下命令记录对文本文件的调用:

RestClient.log = 'log.txt'

这提供了有用但混乱的输出,例如:

RestClient.get "https://dog.ceo/api/breeds/list/all", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"someValue"
# => 200 OK | application/json 1755 bytes

有没有办法使用Awesome Print(或类似的)来格式化这个输出?

4

1 回答 1

0

很好地记录 Restclient 有点棘手,但这里有一些提示,您需要向记录器添加格式化程序,并且 restclient 也存在错误,如您创建记录器的 restclient 文档中所述:

[1] pry(main)> class MyLogger < Logger
  def << (msg)
    debug(msg.strip)
  end
end

[1] pry(main)* => :<<
[6] pry(main)> log = MyLogger.new('my-log.log')
=> #<MyLogger:0x007f8f2f1faf70
 @default_formatter=#<Logger::Formatter:0x007f8f2f1faf20 @datetime_format=nil>,
 @formatter=nil,
 @level=0,
 @logdev=
  #<Logger::LogDevice:0x007f8f2f1faed0
   @dev=#<File:my-log.log>,
   @filename="my-log.log",
   @mon_count=0,
   @mon_mutex=#<Thread::Mutex:0x007f8f2f1fae80>,
   @mon_owner=nil,
   @shift_age=0,
   @shift_period_suffix="%Y%m%d",
   @shift_size=1048576>,
 @progname=nil>
[7] pry(main)> log.info("hola")
=> true
[8] pry(main)> require 'rest-client'
=> true
[12] pry(main)> RestClient.log = log
=> #<MyLogger:0x007f8f2f1faf70
 @default_formatter=#<Logger::Formatter:0x007f8f2f1faf20 @datetime_format=nil>,
 @formatter=nil,
 @level=0,
 @logdev=
  #<Logger::LogDevice:0x007f8f2f1faed0
   @dev=#<File:my-log.log>,
   @filename="my-log.log",
   @mon_count=0,
   @mon_mutex=#<Thread::Mutex:0x007f8f2f1fae80>,
   @mon_owner=nil,
   @shift_age=0,
   @shift_period_suffix="%Y%m%d",
   @shift_size=1048576>,
 @progname=nil>
[15] pry(main)> RestClient.get "https://dog.ceo/api/breeds/list/all", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"someValue"
=> <RestClient::Response 200 "{\"status\":\"...">

这样,您将获得一个名为 my-log.log 的文件,其中包含以下内容:

# Logfile created on 2017-09-22 13:34:11 +0200 by logger.rb/56815
I, [2017-09-22T13:34:27.270103 #17917]  INFO -- : hola
D, [2017-09-22T13:35:45.285204 #17917] DEBUG -- : RestClient.get "https://dog.ceo/api/breeds/list/all", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"someValue"
D, [2017-09-22T13:35:45.466809 #17917] DEBUG -- : # => 200 OK | application/json 1755 bytes

对于另一个格式化程序:

[16] pry(main)> log.formatter = Proc.new{|severity, time, progname, msg|
  formatted_severity = sprintf("%-5s",severity.to_s)
  formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
  "My nice log: [#{formatted_severity} #{formatted_time} #{$$}] #{msg.to_s.strip}\n"
}
[19] pry(main)* => #<Proc:0x007f8f302697a8@(pry):22>
[20] pry(main)> RestClient.get "https://dog.ceo/api/breeds/list/all", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"someValue"

那么你会得到:

My nice log: [DEBUG 2017-09-22 13:48:56 17917] RestClient.get "https://dog.ceo/api/breeds/list/all", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"someValue"
My nice log: [DEBUG 2017-09-22 13:48:56 17917] # => 200 OK | application/json 1755 bytes

要添加很棒的打印,您可以按以下方式工作:require 'awesome_print'

class MyLogger < Logger
  def << (msg)
    ap(msg.strip.red) #redirect to the method added by awesome_print to Logger
  end
end

然后在设置记录器之后,无论有没有.red,你都会得到这个:

D, [2017-09-23T07:41:41.702417 #5160] DEBUG -- : "RestClient.get \"https://dog.ceo/api/breeds/list/all\", \"Accept\"=>\"*/*\", \"Accept-Encoding\"=>\"gzip, deflate\", \"User-Agent\"=>\"someValue\""
D, [2017-09-23T07:41:42.701700 #5160] DEBUG -- : "# => 200 OK | application/json 1755 bytes"
D, [2017-09-23T07:46:28.722936 #5160] DEBUG -- : "\e[1;31mRestClient.get \"https://dog.ceo/api/breeds/list/all\", \"Accept\"=>\"*/*\", \"Accept-Encoding\"=>\"gzip, deflate\", \"User-Agent\"=>\"someValue\"\e[0m"
D, [2017-09-23T07:46:29.037056 #5160] DEBUG -- : "\e[1;31m# => 200 OK | application/json 1755 bytes\e[0m"

在执行代码之前,例如,如果它是一个 ruby​​ on rails 项目,请编写一个名为“loggerrescrlient.rb”的初始化程序。使用以下代码:

formatter = Proc.new{|severity, time, progname, msg|
  formatted_severity = sprintf("%-5s",severity.to_s)
  formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
  "[#{formatted_severity} #{formatted_time} #{$$}] #{msg.to_s.strip}\n"
}

# Add hook for every rest-client request
RestClient.add_before_execution_proc do |req, params|

  Rails.logger.tagged("REST_to_#{req.uri.host}") do
    Rails.logger.info("HTTP request: #{req.uri}")
    Rails.logger.info("HTTP params: #{params[:payload]}")
  end
end

然后每次执行 restlient 时都会打印一个打印好的日志并使用 Rails.logger 打印响应。

于 2017-09-22T10:53:03.780 回答