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
https://github.com/hashicorp/logutils I found this to be very easy to use and you don't even need to change the method calls to log.Printf
of the std library.
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.
Solution 5
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. ;)
pymd
Updated on August 08, 2020Comments
-
pymd over 3 years
Is there any good wrapper available for level based logging in golang? If not, how should I go about implementing one myself?
What I want is pretty simple. I want a few functions e.g.
log.Error() log.Info()
etc that display their outputs to stdout as well as save these in a log file (based on the level given to the program as commandline argument). How do I implement this wrapper?
-
Eric Urban about 10 yearsIt looks as though seelog must acquire/release a global lock for each statement.
-
Friedrich Große almost 10 yearsunfortunately the links are dead
-
neurosnap over 8 yearsAll the links are dead or don't have anything to do with logging.
-
c.s. over 8 yearsSorry for all the movements over time. Needed some time to find the current one as the IMHO best one. The packages are at github.com/tideland/golib and the documentation at godoc.org/?q=tideland%2Fgolib.
-
Larytet over 5 yearsAlso godoc.org/go.uber.org/zap and binary logs github.com/ScottMansfield/nanolog github.com/larytet/binlog
-
Bryan over 5 yearsnanolog does not meet the "display their outputs to stdout" requirement. Binlog has zero GitHub stars.
-
David Buck over 4 yearsA link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
-
Madhu Tomy over 3 yearsone drawback of logrus is that it does n't have log rotation, need to use some external programs for that...
-
BC. over 2 years"The simplest thing that could possibly work" +1
-
codie over 2 yearsA little update from Dec. 2021, zerolog is one hell of an impressive framework github.com/rs/zerolog . A nice alternative to zap.