2017-09-22 38 views
0

当使用REST Client我可以通过登录到一个文本文件调用:如何使用Awesome Print格式化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此输出(或类似)?

+0

输出的响应或所有日志? '需要awesome_print ap File.readlines('log.text')' –

回答

0

这是一个有点棘手测井RESTClient实现,但这里有一些提示,你需要格式化添加到记录器,也有一个bug与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这个conent:

# 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:[email protected](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”的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 

然后在每次执行restlient将被打印以及打印 日志并打印与Rails.logger的响应时间。