How to allow library callers to control logging

117 views
Skip to first unread message

fai...@google.com

unread,
Jul 30, 2020, 9:05:03 PM7/30/20
to golang-nuts
I have a library that is used by cross platform binaries running in corp. Some of these binaries don't necessary run google.Init.

My library is using base/go/log package but this doesn't work when google.Init isn't called. It prints to stderr with a message "ERROR: logging before google.Init". This crashes windows services which crash if there is anything written to stdout or stderr.

My first thought was to have the callers provide a standard log.Logger instance which could be used instead of the base/go/log package. Most binaries could use the base/go/log NewStandardLogger function to get this. While windows services could have a logger instance which just writes out to ioutil.Discard. The downside is I will need multiple *log.Logger instances for different log levels which doesn't seem great.  

Is there a better way to do this? 


leo.b...@npo-data.nl

unread,
Aug 3, 2020, 12:33:11 PM8/3/20
to golang-nuts
Not sure if this is what you are looking for, but https://github.com/go-logr/logr seems to address log abstraction.

Tamás Gulácsi

unread,
Aug 3, 2020, 4:54:29 PM8/3/20
to golang-nuts
I'm using the  go-kit/kit/log (https://pkg.go.dev/github.com/go-kit/kit/log?tab=doc#Logger) abstraction:

    Log(keyvals ...interface{}) error

This can be set in my library, and uses with logfmt, if given.
Reply all
Reply to author
Forward
0 new messages