{"id":253,"date":"2011-05-26T17:49:46","date_gmt":"2011-05-26T15:49:46","guid":{"rendered":"http:\/\/blog.railsware.com\/?p=253"},"modified":"2012-12-08T01:14:25","modified_gmt":"2012-12-07T22:14:25","slug":"logging-external-http-api-calls-in-rails-log","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/logging-external-http-api-calls-in-rails-log\/","title":{"rendered":"Logging external http api calls in Rails log"},"content":{"rendered":"Http api calls are pretty similar to SQL queries &#8211; they query data from external source make it available in ruby. So I think it&#8217;s a good idea to track them in the Rails logger just like ActiveRecord does with SQL queries.\r\n<div style=\"width: 600px; overflow: hidden;\"><a href=\"https:\/\/github.com\/railsware\/http_logger\/raw\/master\/screenshots\/rails_console.png\"><img decoding=\"async\" src=\"https:\/\/github.com\/railsware\/http_logger\/raw\/master\/screenshots\/rails_console.png\" alt=\"example\" \/>\r\n<\/a><\/div>\r\n<!--more-->\r\n\r\nThis simple peace of code could be very helpful in case of debugging HTTP api.\r\nInstallation is pretty simple:\r\n<pre><code>gem install http_logger<\/code><\/pre>\r\nAnd\r\n<pre><code>require 'http_logger' Net::HTTP.logger = Logger.new(...) Net::HTTP.colorize = true <\/code><\/pre>\r\nIn rails you don&#8217;t have to do anything, as defaults are:\r\n<pre><code># Net::HTTP.logger = Rails.logger if defined?(Rails). # Net::HTTP.colorize = true<\/code><\/pre>\r\n&nbsp;\r\n\r\nTODO for v2:\r\n<pre><code>Completed 200 OK in 150ms (Views: 13.5ms | ActiveRecord: 0.0ms | <strong>Net::HTTP: 115.5ms<\/strong>) <\/code><\/pre>\r\n<h3>Offtop for ruby lovers<\/h3>\r\nWant to show the most interesting part in the source code:\r\n<pre><code> def request(request, body = nil, &amp;block) ... <strong>response<\/strong> = request_without_log(request, body, &amp;block) ensure ...... if defined?(<strong>response<\/strong>) rails_log(\"Response body\", <strong>response<\/strong>.body) unless response.body.is_a?(Net::ReadAdapter) end end<\/code><\/pre>\r\nPay attention on how <strong>ensure<\/strong> works with context.\r\nThe <em>response<\/em> local variable defined inside of <em>def<\/em> and <em>ensure<\/em>, but still accessible after ensure if there was no exception inside of <em>request_without_log<\/em>. This would never be possible in static programming language.","protected":false},"excerpt":{"rendered":"<p>Http api calls are pretty similar to SQL queries &#8211; they query data from external source make it available in ruby. So I think it&#8217;s a good idea to track them in the Rails logger just like ActiveRecord does with SQL queries.<\/p>\n","protected":false},"author":15,"featured_media":3735,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Bogdan Gusiev"],"class_list":["post-253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/github.com\/railsware\/http_logger\/raw\/master\/screenshots\/rails_console.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":44,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":13939,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/253\/revisions\/13939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/3735"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=253"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}