production grade logging

Logging to sockets is better than logging to files. It allows for more flexibility in terms of log rotation and data integrity. I started looking around for examples of this but everything these days when it comes to logging is built for the enterprise. The actual skeleton of what all those enterprise systems are doing is quite simple. In fact it is so simple that you can do it in less than 30 lines of code in most high level languages. Here’s the skeleton for a logging server in Ruby:

When a client wants to log some data they connect to /var/run/logger.sock and start writing to it. I’m using line count to do the rotation but you could use any other set of conditions that you want. Extending this to a fully configurable logger is left as an exercise for the reader but even in this skeleton form it is still much better than writing to a file because you no longer have to do the signal and logrotate dance. As soon as your server process connects to the domain socket it just continues to write forever, well at least as long as the logging daemon is running.

Code along with a client can be found at davidk01/ruby-socket-logger