0

I'm writing an API in Sinatra and I want to log the requests that are coming into my service to a table in the database. I want to include the following information:

  • Date/Time
  • Endpoint
  • User-Agent
  • IP Address
  • Payload

What's the best approach here? How can I tap into these and save them off? Maybe there is a gem out there I don't know about.

Craigfoo
  • 313
  • 2
  • 12

1 Answers1

0

I'm sure there is a way to hook this up with Logger, but probably the easiest way to do it would be in an after block, e.g.:

after do
  # assuming you're using something like AR
  LogObject.create({
    time: Time.now,
    req_path: request.path_info
    # ...
  })
end

See How to dump an HTTP request from within Sinatra?, for a bunch of info you can get from the request object.

Community
  • 1
  • 1
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50