...

Source file src/github.com/go-kit/kit/log/level/example_test.go

Documentation: github.com/go-kit/kit/log/level

     1  package level_test
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/go-kit/kit/log"
     8  	"github.com/go-kit/kit/log/level"
     9  )
    10  
    11  func Example_basic() {
    12  	logger := log.NewLogfmtLogger(os.Stdout)
    13  	level.Debug(logger).Log("msg", "this message is at the debug level")
    14  	level.Info(logger).Log("msg", "this message is at the info level")
    15  	level.Warn(logger).Log("msg", "this message is at the warn level")
    16  	level.Error(logger).Log("msg", "this message is at the error level")
    17  
    18  	// Output:
    19  	// level=debug msg="this message is at the debug level"
    20  	// level=info msg="this message is at the info level"
    21  	// level=warn msg="this message is at the warn level"
    22  	// level=error msg="this message is at the error level"
    23  }
    24  
    25  func Example_filtered() {
    26  	// Set up logger with level filter.
    27  	logger := log.NewLogfmtLogger(os.Stdout)
    28  	logger = level.NewFilter(logger, level.AllowInfo())
    29  	logger = log.With(logger, "caller", log.DefaultCaller)
    30  
    31  	// Use level helpers to log at different levels.
    32  	level.Error(logger).Log("err", errors.New("bad data"))
    33  	level.Info(logger).Log("event", "data saved")
    34  	level.Debug(logger).Log("next item", 17) // filtered
    35  
    36  	// Output:
    37  	// level=error caller=example_test.go:32 err="bad data"
    38  	// level=info caller=example_test.go:33 event="data saved"
    39  }
    40  

View as plain text