Recently, I've been thinking about aggregating logs from multiple machines.
Although there are undoubtedly many approaches to this problem, I decided
to write a simple HTTP server which would accept logs and then create
a script which would monitor files for changes and POST them to the HTTP server.
As an experiment I went ahead and did it with node.js because I hadn't
done any projects with it yet, and callbacks struck me as a perfect mechanism for
extending an existing command line tool (in this case piping the output of tail -F
into HTTP requests to a remote server).
First, implementing the log collection server, which simply echos the received
data to stdout.
At this point the log server is only echoing the received logs to standard out, but it would really be preferable if they were being
stored in a file. The simplest way to do this would be to just run the server like this
node logs.js >> centralized.log
but just appending to a file doesn't really allow too much flexibility. Instead, let's throw together a simple mechanism for
allowing users to store the logs in arbitrary ways. We'll use appending to a file as an example backend which can later be
swapped out for another mechanism.
First, replace the existing definition of record_message in logs.js:
The possibilities are endless. Or at least really really broad.
This is already a nice little utility, and it's actually a bit nicer than one might already realize:
while it's been programmed as a fire-and-forget client, the implementation of the node.js
HTTP client is such that it will queue up requests when the host isn't available and then send
them all in the original order when it next attempts to send a message and discovers the
host is available. (This isn't how I expected it work--I expected to manually implement a message
cache for the store-and-forward mechanism--but in this it ends up working fairly well.)
And that's all there is to it. In less than one hundred lines we have a log collecting server with a pluggable storage backend
and a store and forward client which will send changes from monitored files. After doing a few more projects I will definitely
have to write up some thoughts about node.js (admittedly more than a few months late to the game), but I can already say
it's quite a bit more exciting than I had realized: it's really fun to work with,