Skip to content

Instantly share code, notes, and snippets.

@mucaho
Last active November 6, 2025 16:34
Show Gist options
  • Select an option

  • Save mucaho/5f2d58c1b83120ff037a06d1b645e9e5 to your computer and use it in GitHub Desktop.

Select an option

Save mucaho/5f2d58c1b83120ff037a06d1b645e9e5 to your computer and use it in GitHub Desktop.
Ruby net/http logging
cd ~/.asdf/installs/ruby/3.2.6/lib/ruby/3.2.0/net
git apply --directory=. /path/to/http_loggers.patch

Trigger network requests

cd /rails/root/directory
cat log/development.log | grep -A3 "\[HTTP_"
--- a/~/.asdf/installs/ruby/3.2.6/lib/ruby/3.2.0/net/http.rb
+++ b/~/.asdf/installs/ruby/3.2.6/lib/ruby/3.2.0/net/http.rb
@@ -1912,6 +1912,11 @@
# - Net::HTTP.get: sends GET request, returns response body.
#
def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
res = nil
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_GET]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
request(Get.new(path, initheader)) {|r|
r.read_body dest, &block
res = r
@@ -1941,6 +1946,11 @@
# ["connection", ["close"]]]
#
def head(path, initheader = nil)
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_HEAD]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
request(Head.new(path, initheader))
end
@@ -1980,6 +1990,12 @@
# - Net::HTTP.post: sends POST request, returns response body.
#
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_POST]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " data: #{data.inspect}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
send_entity(path, data, initheader, dest, Post, &block)
end
@@ -2015,6 +2031,12 @@
# http.patch('/todos/1', data) # => #<Net::HTTPCreated 201 Created readbody=true>
#
def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_PATCH]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " data: #{data.inspect}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
send_entity(path, data, initheader, dest, Patch, &block)
end
@@ -2035,6 +2057,12 @@
# http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
#
def put(path, data, initheader = nil)
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_PUT]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " data: #{data.inspect}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
request(Put.new(path, initheader), data)
end
@@ -2118,6 +2146,11 @@
# http.delete('/todos/1')
#
def delete(path, initheader = {'Depth' => 'Infinity'})
+ # THIS LOGGER WAS ADDED TEMPORARILY, REMOVE AFTER TESTING
+ Rails.logger.warn("[HTTP_DELETE]: #{self.class.name}.#{__method__} called\n" \
+ " path: #{path}\n" \
+ " backtrace: #{Rails.backtrace_cleaner.clean(caller).join('|')}")
+
request(Delete.new(path, initheader))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment