How to implement level based logging in golang?

78,869

Solution 1

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

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. ;)

Share:
78,869
pymd
Author by

pymd

Updated on August 08, 2020

Comments

  • pymd
    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
    Eric Urban about 10 years
    It looks as though seelog must acquire/release a global lock for each statement.
  • Friedrich Große
    Friedrich Große almost 10 years
    unfortunately the links are dead
  • neurosnap
    neurosnap over 8 years
    All the links are dead or don't have anything to do with logging.
  • c.s.
    c.s. over 8 years
    Sorry 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
    Larytet over 5 years
  • Bryan
    Bryan over 5 years
    nanolog does not meet the "display their outputs to stdout" requirement. Binlog has zero GitHub stars.
  • David Buck
    David Buck over 4 years
    A 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
    Madhu Tomy over 3 years
    one drawback of logrus is that it does n't have log rotation, need to use some external programs for that...
  • BC.
    BC. over 2 years
    "The simplest thing that could possibly work" +1
  • codie
    codie over 2 years
    A little update from Dec. 2021, zerolog is one hell of an impressive framework github.com/rs/zerolog . A nice alternative to zap.