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



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

井lotclient有点棘手,但这里有一些提示,您需要在logger中添加一个格式化器,而且还有一个带有RESTCLIENT的错误,如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 bytese[0m"

执行代码之前,例如,如果它是Ruby在Rails项目上,请撰写一个名为" LoggerRescrlient.RB"的Initilizer。使用以下代码:

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

然后,每次执行剩余时间都会打印出井打印并用rails.logger打印响应。

最新更新