How to implement level based logging in golang?

Solution 1:

Some more suggestions, now that the existing answers are quite old:

  • https://github.com/op/go-logging - smaller than the other here
  • https://github.com/sirupsen/logrus - used in many popular projects such as Docker
  • https://github.com/inconshreveable/log15
  • https://github.com/golang/glog - from Google, implementation of their C++ glog library in Go
  • https://github.com/go-kit/kit/tree/master/log focused on "structured logging" which is better for tools to consume
  • https://github.com/uber-go/zap - "blazing fast"

Solution 2:

  • Uber-go/Zap: Fast, structured, leveled logging in Go
  • Logrus: Structured, pluggable logging for Go. (JSON and text formatting)

Both libraries have level hooks also, which is a very interesting feature. Hooks can be registered for particular log levels. So for example any error(logged using log.Error()) occurs you can report to some monitoring tool etc.

Solution 3:

Take a look at http://cgl.tideland.biz and there at the package "applog". It's working that way.

PS: The whole CGL is currently reworked and will soon be released with new features, but under a different name. ;)

Solution 4:

I think seelog fits your requirements, and it seems to be pretty popular as it pops up often in log discussions. I never used it seriously, so I can't comment beyond that.