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?
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.Uber-go/Zap: Fast, structured, leveled logging in Go
I am working with rlog at the moment and like their approach. The code looks good, simplistic and sufficiently documented.
What convinced me:
rlog.Info()
anywhere without passing around referencesrlog.Trace(4, "foo")
Some more suggestions, now that the existing answers are quite old:
I have added logging level support to the built-in Go log package. You can find my code here:
https://github.com/gologme/log
In addition to adding support for Info, Warn, and Debug, users can also define their own arbitrary logging levels. Logging levels are enabled and disabled individually. This means you can turn on Debug logs without also getting everything else.
stdlog fits exactly your requirements: