Fog Creek

There are Gophers with Gifts at Fog Creek

That’s right, several Kiwis were bitten by Gophers and are now Kiwi-Gopher hybrids. In other words, Fog Creek has started using Go. As part of that initiative we have some gifts for you – two libraries that we are sharing with the community. The first library, Mini, is a small package for reading .ini style configuration files. The second package is a bit larger and implements a tagged style of logging.

Why We Wrote the Packages

Creating Mini

So, you might ask, why did we write these two packages in the first place? Well, for the ini config reader it’s easy – there wasn’t a package with the features we needed when we started the project. Maybe we could have limped along, but creating our own package was also a good way to get some experience with Go and go test. Speaking of go test, the Mini package has 100% test coverage, a pretty cool milestone.

It’s available on GitHub, and documented on GoDoc.

Tagged Logging

Writing the logging package required a bit more motivation. Go already had a logging package and there were a few others in the wild too. But our motivation sprang from a desire to implement a concept I call tagged logging. Tagged logging is the idea that you can set log levels based on tags, not just as a default value or specific named logger. With tagged logging, each call to the logging package can have multiple mechanisms for determining if the message should be output.

A great example of this is in a project we are working on that wraps our search needs in a HTTP service. When we receive a HTTP request, we note the account associated with the request. Then, we use that account as a tag for each log message generated while we process it. If a particular account is having problems, we can activate debug logging for that account by enabling debug logging for a tag whose value is that account. This allows us to debug specific accounts in production without restarting the service.

In simpler cases, we also tag HTTP code as ‘http’ and Elasticsearch code as ‘elastic’, so we can turn on debug logging orthogonally from the location of the code. The same function can contain log messages tagged as ‘http’ or ‘elastic’, but with the same ‘account’.

So in the code below we can turn on debug for ‘http’, ‘elastic’ or ‘account: 1’ to pick which log messages we want to see:

httpTags:=[]string{"http", "account:1"}
elasticTags:=[]string{"elastic", "account:1"}
...
logger.DebugWithTags(httpTags, "http verbosity")
...
logger.DebugWithTags(elasticTags, "elastic verbosity")

A few other useful features in the logging library are rolled log files, background logging with channels and support for custom appenders. Oh, and in case you are wondering, the logging package is at 88% coverage. We are still learning how to get tests to poke into some of those hard to reach places, like Syslog.

We are just getting a handle on tagged logging, and we hope you give it a try and let us know how it goes for you too. The logging package is also available on GitHub and GoDoc.

Now, no-one ever said to beware of Gophers bearing gifts, so all’s good. Happy coding!