Skip to content

Instantly share code, notes, and snippets.

Last active January 31, 2017 16:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
package level //
type LogLevels int
const (
Debug LogLevels = iota
// main.go....
func main() {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
// A (no leveled logging)
logger.Log(Level.Debug, "foo", "bar")
// level=debug foo=bar
// B
l = logger.WithFilter(level.Filter(level.Debug)) // WithFilter returns a log.Logger
l.Log(level.Debug, "foo", "bar")
// foo=bar
// C
l = logger.WithFilter(level.Filter(level.Info))
l.Log(Level.Debug, "foo","bar")
// <nothing>
// D
l = logger.With(Level.Info)
l = l.WithFilter(level.Filter(level.Info))
// pass l to a library/struct that doesn't use levels to fix all logs from that library to Info
// Assuming that that library/struct eventually starts to use levels (you submitted a PR right?)
// This still works as the locally applied levels could take precedence over the global as the global
// still applies the filter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment