그것은 잘 위해 RESTClient 로그에 조금 까다로운 그러나 여기 당신이 로거에 포맷터를 추가해야하고 또한 위해 RESTClient의 문서에 말했듯이 당신이 로거를 작성, 위해 RESTClient와 bug가, 몇 가지 팁입니다 :
# 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
다른 포맷에 대한 :
이와
[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\":\"...">
당신은 파일이 conent 내-Log.log를 호출되는 것입니다 33,210
당신은 얻을 것이다 :
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
끝내 인쇄를 추가하려면 다음과 같이 작업 할 수 이
class MyLogger < Logger
def << (msg)
ap(msg.strip.red) #redirect to the method added by awesome_print to Logger
end
end
다음 로거를 설정 한 후 당신이 이것을 얻을 것이다 'awesome_print'를 요구하거나 .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"
코드를 실행하기 전에 시험 만약 그것이 루비 레일 프로젝트라면 "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와 응답을 인쇄한다 실행할 때마다.
응답 또는 모든 로그 출력? 'awesome_print ap File.readlines ('log.text')'가 필요합니다. –