如何使用Awesome Print格式化REST客户端日志?

时间:2017-09-22 08:24:00

标签: ruby rest-client awesomeprint

使用REST Client时,我可以使用以下方式记录对文本文件的调用:

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(或类似的)格式化此输出?

1 个答案:

答案 0 :(得分:0)

熟悉Restclient有点棘手但是这里有一些提示,你需要在记录器中添加一个格式化程序,并且还有一个带有restclient的bug,如在restclient doc中所说的那样你创建一个记录器用:

[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

添加精美的打印件,您可以按照以下方式工作:     要求'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"

在执行代码之前, 例如,如果它是rails上的ruby项目,则编写一个名为“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打印响应。