Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging API calls

Tags:

php

api

I've written an API and currently am looking at what is the best way to provide logging for the system.

We want to be able to have an overview of the calls being made, if particular calls are being hit very often and such.

What is the best way to log and present all this information simply?

Simply wondering if there are any pre-built or commonly used solutions with APIs that encompass what I am looking for.

like image 317
James Avatar asked Oct 22 '10 05:10

James


People also ask

How do I log API calls?

A simple solution would be to log all calls to a file, maybe in a format similar to Apache web server logs. Then you can parse the log with an existing log analytics tool,for example Webalizer. Show activity on this post. Logs are a stream, and it behooves everyone to treat them as such.

Should you log API calls?

Please Log your API requests and our responses First, we recommend that you always log the HTTP response code that you get from each request. This will allow you to look back and find out if there was a problem with a particular request, or to trigger automatic retries.

How do I get an API log?

You can search over the full_request_path of an API Log event. Use this to specify a particular endpoint, e.g. analytics: /api/as/v1/engines/[ENGINE]/click. json , or search: /api/as/v1/engines/[ENGINE]/search.


2 Answers

There are several ways you could go about doing this. I would start with an abstracted logging solution first, something like Monolog [https://github.com/Seldaek/monolog]. This way you can test and play with what you want to log in a text file to start with then move to a more elegant solution.

Monolog has Handlers for a bunch of different ligging services/containers. For API usage logging I would take a look at the following:

GrayLog2, a really nice Open Source logging system.

Cube, also a great Open Source project for collecting timestamped events.

Loggly, A commercial Cloud-based logging platform. Monolog does not have a handler for Loggly right now but I wrote one that I'm currently using in production. My fork of the Monolog project has this handler in it, I plan on submitting a pull request soon : Download on Gitub here.

Sentry, a realtime event logging and aggregation platform that you can host yourself or use the paid hosted version. Like Loggly, no handler in Monolog for this but it is a really nice project with a nice API you can easily write some simple code to log to.

like image 164
rbl00 Avatar answered Oct 06 '22 17:10

rbl00


A simple solution would be to log all calls to a file, maybe in a format similar to Apache web server logs. Then you can parse the log with an existing log analytics tool,for example Webalizer.

like image 30
Emil Vikström Avatar answered Oct 06 '22 17:10

Emil Vikström