Posted by tobi — 03:00 PM Dec 06
Log files are one of the most important aspects of any web service. A webapp with a well designed logging strategy will allow you to essentially go back in time to track down even the most obscure bug. Unit tests have diminished the importance of log files somewhat but how do you write a unit test for a bug that only happens on server 2 around 2am in the morning when user fred is logged in?
Most of us run exception notification services such as exception_logger, hoptoad, exceptional or simply but log exceptions to a DB table ( MySQL protip: make that table MyISAM, otherwise exceptions that are added during a transaction will be removed when that transaction rolls back – duh ). Those exception notifications are great but they never provide a lot of context for how the user got to the point.
Lastly there is also the role that log files play in customer support. Have you ever gotten a complaint about data disappearing from your service? With good logging you can tell your customer in a matter of minutes that employee Bob went on data rampage friday evening before handing in his resignation.
At Shopify we use syslog-ng to have a centralized logging server which collects all the logs from the various machines in our cluster and combines the log files together. We used to give everyone access to this box for log analysis but as we grew this became a bit impractical. To solve this, we created Clarity, which provides a very nice web interface for the two staple tools of log analysis: grep and tail -f.
Clarity is very lightweight and only requires a few dependencies such as eventmachine and json. It’s completely evented which means that you can have many different greps and tails running at the same time on a single instance (as much as the server can handle). It even stops the grep utility on the server when you hit stop in your browser.
~ $ sudo gem install clarity
Password:
Successfully installed clarity-0.9.8
1 gem installed
Installing ri documentation for clarity-0.9.8...
Installing RDoc documentation for clarity-0.9.8...
Could not find main page README.rdoc # anyone know how to get rid of this?!
Could not find main page README.rdoc
Could not find main page README.rdoc
Could not find main page README.rdoc
~ $ clarity /var/log --include '*/**'
Clarity 0.9.8 starting up.
* listening on 0.0.0.0:8080
* Log mask(s): */**
Additional command line parameters are:
~ $ clarity --help
Usage: clarity [options] directory
Specific options:
-p, --port=PORT Port to listen on
-b, --address=ADDRESS Address to bind to (default 0.0.0.0)
--include=MASK File mask of logs to add (default: **/*.log*)
--user=USER User to run as
Password protection:
--username=USER Enable httpauth username
--password=PASS Enable httpauth password
Misc:
-h, --help Show this message.
Clarity is now used by our support staff on a daily basis. It’s been so successful internally that we decided to release it as open source. You can read more about it on the github page. The URL structure of Clarity is pretty simple so it’s easy to add links to your internal admin area that directly open log files with the appropriate terms prefilled. You can for example add a link to a search that shows you all the DELETE requests of a certain store directly to your support system. This means that blaming Bob will only take one click in the future.

Kent Fenwick 06 Dec 18:06
Awesome! Thanks for this.
Let’s hope that some great services like Engine Yard Cloud pick this up and integrate into their products.
Thanks again.
Olly 08 Dec 11:32
Would it be possible to post instructions on how to get Clarity up and running via Apache?
Paul Stamatiou 10 Dec 03:11
Interesting – thanks for releasing it. I was playing with it tonight and I was thinking it would be quite handy to support commands like this
tail -f log/development.log | grep -A 10 -B 10 “Mime-version”
For example in that I like to see the live log as I do things in the application that send emails so I look for it in the log, but with clarity I would only get a list of “Mime-version” instead of its context as well, so the A & B flags are very handy.
I haven’t dug around too much or looked at many forks so chances are this is already possible – my apologies if so!
Best, Paul
windump 12 Dec 10:16
any idea how to port this line of code to work on a MS Windows ruby platform?
~ I’ve tried a few things but keep getting this: Running: sh -c ‘grep -e hello access.log’
This application has requested the Runtime to terminate it in an unusual way. Please contact the application’s support team for more information.