-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
39 lines (32 loc) · 822 Bytes
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)
type ctxLogger struct{}
func newLogger() *log.Logger {
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stdout)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
if !*debug {
logger = level.NewFilter(logger, level.AllowInfo())
}
}
return &logger
}
// ContextWithLogger adds logger to context
func contextWithLogger(ctx context.Context, l *log.Logger) context.Context {
return context.WithValue(ctx, ctxLogger{}, l)
}
func loggerFromContext(ctx context.Context) log.Logger {
if l, ok := ctx.Value(ctxLogger{}).(*log.Logger); ok {
return *l
}
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
return logger
}