• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H02-Jun-2021-

level/H02-Jun-2021-

syslog/H02-Jun-2021-

term/H02-Jun-2021-

.gitignoreH A D02-Jun-2021269

LICENSEH A D02-Jun-20211 KiB

README.mdH A D02-Jun-20214.7 KiB

benchmark_test.goH A D02-Jun-2021443

concurrency_test.goH A D02-Jun-2021653

doc.goH A D02-Jun-20215.2 KiB

example_test.goH A D02-Jun-20212.7 KiB

go.modH A D02-Jun-2021121

go.sumH A D02-Jun-2021342

json_logger.goH A D02-Jun-20212 KiB

json_logger_test.goH A D02-Jun-20213.8 KiB

log.goH A D02-Jun-20216.3 KiB

log_test.goH A D02-Jun-20218.2 KiB

logfmt_logger.goH A D02-Jun-20211.3 KiB

logfmt_logger_test.goH A D02-Jun-20211.4 KiB

nop_logger.goH A D02-Jun-2021206

nop_logger_test.goH A D02-Jun-2021502

stdlib.goH A D02-Jun-20214.2 KiB

stdlib_test.goH A D02-Jun-20219.3 KiB

sync.goH A D02-Jun-20213.2 KiB

sync_test.goH A D02-Jun-20212 KiB

value.goH A D02-Jun-20213.3 KiB

value_test.goH A D02-Jun-20213.8 KiB

README.md

1# package log
2
3`package log` provides a minimal interface for structured logging in services.
4It may be wrapped to encode conventions, enforce type-safety, provide leveled
5logging, and so on. It can be used for both typical application log events,
6and log-structured data streams.
7
8## Structured logging
9
10Structured logging is, basically, conceding to the reality that logs are
11_data_, and warrant some level of schematic rigor. Using a stricter,
12key/value-oriented message format for our logs, containing contextual and
13semantic information, makes it much easier to get insight into the
14operational activity of the systems we build. Consequently, `package log` is
15of the strong belief that "[the benefits of structured logging outweigh the
16minimal effort involved](https://www.thoughtworks.com/radar/techniques/structured-logging)".
17
18Migrating from unstructured to structured logging is probably a lot easier
19than you'd expect.
20
21```go
22// Unstructured
23log.Printf("HTTP server listening on %s", addr)
24
25// Structured
26logger.Log("transport", "HTTP", "addr", addr, "msg", "listening")
27```
28
29## Usage
30
31### Typical application logging
32
33```go
34w := log.NewSyncWriter(os.Stderr)
35logger := log.NewLogfmtLogger(w)
36logger.Log("question", "what is the meaning of life?", "answer", 42)
37
38// Output:
39// question="what is the meaning of life?" answer=42
40```
41
42### Contextual Loggers
43
44```go
45func main() {
46	var logger log.Logger
47	logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
48	logger = log.With(logger, "instance_id", 123)
49
50	logger.Log("msg", "starting")
51	NewWorker(log.With(logger, "component", "worker")).Run()
52	NewSlacker(log.With(logger, "component", "slacker")).Run()
53}
54
55// Output:
56// instance_id=123 msg=starting
57// instance_id=123 component=worker msg=running
58// instance_id=123 component=slacker msg=running
59```
60
61### Interact with stdlib logger
62
63Redirect stdlib logger to Go kit logger.
64
65```go
66import (
67	"os"
68	stdlog "log"
69	kitlog "github.com/go-kit/log"
70)
71
72func main() {
73	logger := kitlog.NewJSONLogger(kitlog.NewSyncWriter(os.Stdout))
74	stdlog.SetOutput(kitlog.NewStdlibAdapter(logger))
75	stdlog.Print("I sure like pie")
76}
77
78// Output:
79// {"msg":"I sure like pie","ts":"2016/01/01 12:34:56"}
80```
81
82Or, if, for legacy reasons, you need to pipe all of your logging through the
83stdlib log package, you can redirect Go kit logger to the stdlib logger.
84
85```go
86logger := kitlog.NewLogfmtLogger(kitlog.StdlibWriter{})
87logger.Log("legacy", true, "msg", "at least it's something")
88
89// Output:
90// 2016/01/01 12:34:56 legacy=true msg="at least it's something"
91```
92
93### Timestamps and callers
94
95```go
96var logger log.Logger
97logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
98logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
99
100logger.Log("msg", "hello")
101
102// Output:
103// ts=2016-01-01T12:34:56Z caller=main.go:15 msg=hello
104```
105
106## Levels
107
108Log levels are supported via the [level package](https://godoc.org/github.com/go-kit/log/level).
109
110## Supported output formats
111
112- [Logfmt](https://brandur.org/logfmt) ([see also](https://blog.codeship.com/logfmt-a-log-format-thats-easy-to-read-and-write))
113- JSON
114
115## Enhancements
116
117`package log` is centered on the one-method Logger interface.
118
119```go
120type Logger interface {
121	Log(keyvals ...interface{}) error
122}
123```
124
125This interface, and its supporting code like is the product of much iteration
126and evaluation. For more details on the evolution of the Logger interface,
127see [The Hunt for a Logger Interface](http://go-talks.appspot.com/github.com/ChrisHines/talks/structured-logging/structured-logging.slide#1),
128a talk by [Chris Hines](https://github.com/ChrisHines).
129Also, please see
130[#63](https://github.com/go-kit/kit/issues/63),
131[#76](https://github.com/go-kit/kit/pull/76),
132[#131](https://github.com/go-kit/kit/issues/131),
133[#157](https://github.com/go-kit/kit/pull/157),
134[#164](https://github.com/go-kit/kit/issues/164), and
135[#252](https://github.com/go-kit/kit/pull/252)
136to review historical conversations about package log and the Logger interface.
137
138Value-add packages and suggestions,
139like improvements to [the leveled logger](https://godoc.org/github.com/go-kit/log/level),
140are of course welcome. Good proposals should
141
142- Be composable with [contextual loggers](https://godoc.org/github.com/go-kit/log#With),
143- Not break the behavior of [log.Caller](https://godoc.org/github.com/go-kit/log#Caller) in any wrapped contextual loggers, and
144- Be friendly to packages that accept only an unadorned log.Logger.
145
146## Benchmarks & comparisons
147
148There are a few Go logging benchmarks and comparisons that include Go kit's package log.
149
150- [imkira/go-loggers-bench](https://github.com/imkira/go-loggers-bench) includes kit/log
151- [uber-common/zap](https://github.com/uber-common/zap), a zero-alloc logging library, includes a comparison with kit/log
152