Thursday, February 07, 2008

New logging framework

The current logging code used by the HTTP server and a few other vocabularies is too simplistic. There is too much boilerplate; you have to log messages by constructing strings and then calling log-message explicitly. Also, the log files eventually get quite large since there is no support for log rotation. Finally the format is quite ad-hoc and there are no tools for parsing them.

Today I wrote a new logging framework, aptly named extra/logging. Quite a lot of code for one day - 407 lines. The framework is quite powerful; the main features:
  • Log rotation prevents log files from getting too large
  • Machine-parsable log file format with an included parser which uses parser combinators.
  • Log analysis and reports, using said parser
  • Optional nightly log rotation and e-mailing of summaries

Since it is now in the git repository and fully documented (just do
"logging" require   "logging" about
) I won't bother describing it fully here, but suffice to say it is quite a nice framework and I'm looking forward to using it to run the Factor web site and receiving daily e-mails with hit counts.

The design is interesting; it uses Chris Double's Erlang-style concurrency library to implement a concurrent server which listens for messages that either instruct it to log a message or rotate the logs.

The user-visible words send a message to the server; users of the logging library never deal with message passing directly. The server handles opening and closing log streams; message passing here ensures that the server is a synchronization point is multiple threads can write to the same log file in an orderly fashion; it also ensures that log rotation can proceed with all logging temporarily suspended.

Incidentally, the SMTP library used for mailing log summaries was developed by Elie Chaftari and yesterday I refactored it a bit and added some features for Eduardo's upcoming continuous integration system, which will send e-mails when daily builds fail. Then I came up with the idea of having a message-passing logging library, and decided to implement it right away; the lack of log rotation is a pain on right now.

No comments: