func TestContextLevel(t *testing.T) { var buf bytes.Buffer // Wrapping a context with the level logger still works, but requires users // to specify a higher callstack depth value. var logger log.Logger logger = log.NewLogfmtLogger(&buf) logger = log.NewContext(logger).With("caller", log.Caller(5)) logger = level.New(logger, level.Config{Allowed: level.AllowAll()}) level.Info(logger).Log("foo", "bar") if want, have := `caller=level_test.go:150 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have { t.Errorf("want %q, have %q", want, have) } }
func init() { termLog = log.NewLogfmtLogger(os.Stderr) if term.IsTerminal(os.Stderr) { termLog = term.NewLogger( os.Stderr, log.NewLogfmtLogger, kithlp.NewLevelColorer("level", nil), ) } logBase.Swap(termLog) lg := kithlp.StringifyLogger{logBase} Logger = log.NewContext(lg).With("ts", log.DefaultTimestampUTC, "caller", log.Caller(4)) logger = levels.New(Logger) stdlog.SetOutput(log.NewStdlibAdapter(logBase)) converter.Logger.Swap(Logger) }
// application. InfoLevel // DebugLevel level. Usually only enabled when debugging. Very verbose // logging. DebugLevel ) // currentLevel has a default and it's updated later by levelFlag var currentLevel = InfoLevel var ( logger = log.NewLogfmtLogger(os.Stderr) ctx = log.NewContext(logger).With("ts", log.DefaultTimestampUTC) // Logger is the global application logger Logger = levels.New(ctx).With("caller", log.Caller(5)) ) type levelFlag struct{} // String implements flag.Value. func (f levelFlag) String() string { return fmt.Sprint(currentLevel) } // Set implements flag.Value. func (f levelFlag) Set(level string) (err error) { currentLevel, err = ParseLevel(level) return err }