Skip to content

Commit

Permalink
log/level: improve examples (#845)
Browse files Browse the repository at this point in the history
* log/level: improve examples

* log: mention package level in README

* log/level: example line numbers changed
  • Loading branch information
peterbourgon authored Mar 5, 2019
1 parent 102522a commit 15ff8c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 18 additions & 4 deletions log/level/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 15ff8c2

Please sign in to comment.