Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/level: improve examples #845

Merged
merged 3 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}