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.
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.
If you are exposing APIs you can log the requests you receive. Use the following Middleware:
config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware
Check the README for further options.
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)
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)
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 ⭐️!