go-logging icon indicating copy to clipboard operation
go-logging copied to clipboard

*Logger type uses global backend in IsEnabledFor method

Open triztian opened this issue 6 years ago • 0 comments

Whenever a new logger is created; it defaults to calling the global defaultBackend rather than it's own backend.

  • https://github.com/op/go-logging/blob/master/logger.go#L138
func (l *Logger) IsEnabledFor(level Level) bool {
	return defaultBackend.IsEnabledFor(level, l.Module)
}

This causes for the backend defined by the SetBackend method to be ignored unless set with the package method SetBackend; i.e:

package main

import (
    "os"
    "github.com/op/go-logging"
)

func main() {

    formatter := logging.MustStringFormatter("JUST A PREFIX: ${message}")
    backend := logging.NewLogBackend(os.Stdout, "", log.LstdFlags)
    lg := logging.MustGetLogger("example")
    backendFormatter := logging.NewBackendFormatter(backend, formatter)
    
    leveled := logging.AddModuleLevel(backendFormatter)
    leveled.SetLevel(logging.DEBUG, "")

    // The following will cause for the backend `leveled` to be ignored by `lg`
    lg.SetBackend(leveled)
    
    // This will set the backend globally
    logging.SetBackend(leveled)
}

triztian avatar May 01 '18 15:05 triztian