DEV Community

loading...

Log API requests in Rails

coorasse profile image Alessandro Rodi Updated on ・1 min read

When exposing or consuming APIs, one of the most important things to remember is to log the calls you perform.

It will not take long until you will have to answer the question "What happened? And why?".

Having a proper log of the API requests you performed or received is the only way to answer these questions.

That's why at Renuo, we extracted rails_api_logger from our projects and made it open source.

rails_api_logger is a library that can be used for logging both inbound requests and outbound requests.

Please read the README file for a full documentation. Here I will sum it up quickly.

Log Inbound Requests

If you are exposing APIs you can log the requests you receive. Use the following Middleware:

config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware

Enter fullscreen mode Exit fullscreen mode

Check the README for further options.

Log Outbound Requests

Given the following outbound request:

uri = URI('http://example.com/some_path?query=string')
http = Net::HTTP.start(uri.host, uri.port)
request = Net::HTTP::Get.new(uri)
response = http.request(request)
Enter fullscreen mode Exit fullscreen mode

you can log it by doing the following:

uri = URI('http://example.com/some_path?query=string')
http = Net::HTTP.start(uri.host, uri.port)
request = Net::HTTP::Get.new(uri)
response = RailsApiLogger.call(uri, http, request)
Enter fullscreen mode Exit fullscreen mode

The gem comes with a first code example to integrate these logs in RailsAdmin. It the future we will release more and more features.

Head to rails_api_logger on GitHub and give it a ⭐️!

Discussion (0)

pic
Editor guide