'Logging API calls

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.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

Consider approach log is a stream

Logs are a stream, and it behooves everyone to treat them as such. Your programs should log to stdout and/or stderr and omit any attempt to handle log paths, log rotation, or sending logs over the syslog protocol. Directing where the program’s log stream goes can be left up to the runtime container: a local terminal or IDE (in development environments), an Upstart / Systemd launch script (in traditional hosting environments), or a system like Logplex/Heroku (in a platform environment).

(I noticed it in this answer, which is originally from this article)

Good thing is marked in @Emil Vikström's answer, which is need to notice, is a format to simplify parsing and analyzing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rbl00
Solution 2 Emil Vikström
Solution 3 oklas