From 15ff8c232d32c98141e2042e30e4047118fee9cf Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 5 Mar 2019 13:16:00 -0800 Subject: [PATCH] log/level: improve examples (#845) * log/level: improve examples * log: mention package level in README * log/level: example line numbers changed --- log/README.md | 4 ++++ log/level/example_test.go | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/log/README.md b/log/README.md index 7222f8009..a201a3d92 100644 --- a/log/README.md +++ b/log/README.md @@ -103,6 +103,10 @@ logger.Log("msg", "hello") // ts=2016-01-01T12:34:56Z caller=main.go:15 msg=hello ``` +## Levels + +Log levels are supported via the [level package](https://godoc.org/github.com/go-kit/kit/log/level). + ## Supported output formats - [Logfmt](https://brandur.org/logfmt) ([see also](https://blog.codeship.com/logfmt-a-log-format-thats-easy-to-read-and-write)) diff --git a/log/level/example_test.go b/log/level/example_test.go index fed52e512..73cc752a3 100644 --- a/log/level/example_test.go +++ b/log/level/example_test.go @@ -9,17 +9,31 @@ import ( ) func Example_basic() { - // setup logger with level filter + logger := log.NewLogfmtLogger(os.Stdout) + level.Debug(logger).Log("msg", "this message is at thte debug level") + level.Info(logger).Log("msg", "this message is at the info level") + level.Warn(logger).Log("msg", "this message is at the warn level") + level.Error(logger).Log("msg", "this message is at the error level") + + // Output: + // level=debug msg="this message is at thte debug level" + // level=info msg="this message is at the info level" + // level=warn msg="this message is at the warn level" + // level=error msg="this message is at the error level" +} + +func Example_filtered() { + // Set up logger with level filter. logger := log.NewLogfmtLogger(os.Stdout) logger = level.NewFilter(logger, level.AllowInfo()) logger = log.With(logger, "caller", log.DefaultCaller) - // use level helpers to log at different levels + // Use level helpers to log at different levels. level.Error(logger).Log("err", errors.New("bad data")) level.Info(logger).Log("event", "data saved") level.Debug(logger).Log("next item", 17) // filtered // Output: - // level=error caller=example_test.go:18 err="bad data" - // level=info caller=example_test.go:19 event="data saved" + // level=error caller=example_test.go:32 err="bad data" + // level=info caller=example_test.go:33 event="data saved" }