Last active
February 1, 2019 13:21
-
-
Save DimaSamodurov/242799dc8f07074429474a0d1d840754 to your computer and use it in GitHub Desktop.
Simple logger for Ruby Net::HTTP requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
# The simplified version of logger can be copypasted into Rails initializer. | |
module HttpLogger | |
def request(req, body = nil, &block) | |
start_time = Time.current | |
super.tap do |resp| | |
log_request(http: self, request: req, response: resp, start_time: start_time) rescue nil | |
end | |
end | |
private | |
def log_request(http:, request:, response:, start_time:) | |
request_duration = ((Time.current - start_time) * 1000).round | |
request_url = | |
URI.decode("http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}") | |
Rails.logger.info(name: 'http_request', | |
method: request.method, | |
url: request_url, | |
status: response.code, | |
duration: request_duration) | |
end | |
end | |
Net::HTTP.prepend HttpLogger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
module HttpLogger | |
module DSL | |
def activate | |
Net::HTTP.prepend HttpLogger | |
end | |
def activated? | |
Net::HTTP.ancestors.include? HttpLogger | |
end | |
# Register a callback that will be called upon request completion. | |
# Callback will receive following parameters: | |
# http: HTTP instance, | |
# request: HTTP Request, | |
# response: HTTP Response, | |
# start_time: Request Start Time (with time zone) | |
# Example: | |
# HttpLogger.after_request do |http:, request:, response:, start_time:| | |
# # log request details | |
# end | |
def after_request(&block) | |
after_request_callbacks << block | |
end | |
def after_request_callbacks | |
@after_request_callbacks ||= [] | |
end | |
end | |
extend DSL | |
def request(req, body = nil, &block) | |
start_time = Time.current | |
super.tap do |resp| | |
HttpLogger.after_request_callbacks.each do |callback| | |
callback.call(http: self, request: req, response: resp, start_time: start_time) | |
end | |
end | |
end | |
end | |
# The following can be put e.g. to Rails initializer. | |
HttpLogger.activate | |
HttpLogger.after_request do |http:, request:, response:, start_time:| | |
request_duration = (Time.current - start_time).round(3) | |
request_url = | |
URI.decode("http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}") | |
Rails.logger.info(name: 'http_request', | |
method: request.method, | |
url: request_url, | |
status: response.code, | |
duration: request_duration) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment