-
Notifications
You must be signed in to change notification settings - Fork 2
/
extrazap.go
41 lines (35 loc) · 1.44 KB
/
extrazap.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
40
41
// Package extrazap provides helper functions for initializing Zap loggers,
// as well as associating and extracting them with/from context objects.
package extrazap
import (
"context"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ctxKey struct{}
// NewDevelopmentLogger builds a customized development logger which is even
// more user-friendly than [zap.NewDevelopment].
func NewDevelopmentLogger() *zap.Logger {
c := zap.NewDevelopmentConfig()
c.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
c.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
// TODO: Pretty-print tags like zerolog, instead of a JSON string?
// TODO: Color "error" and "Error" tag values in red, like zerolog?
// cfg.EncoderConfig.ConsoleSeparator = " "
// TODO: Check out https://github.com/charmbracelet/log
return zap.Must(c.Build())
}
// AttachLoggerToContext returns a copy of the given context with the given logger
// attached to it. Neither the given context nor the given logger are affected.
func AttachLoggerToContext(l *zap.Logger, ctx context.Context) context.Context {
return context.WithValue(ctx, ctxKey{}, l)
}
// ExtractLoggerFromContext returns the logger attached to the given context,
// or the global logger if no logger instance was previously attached to it.
func ExtractLoggerFromContext(ctx context.Context) *zap.Logger {
if l, ok := ctx.Value(ctxKey{}).(*zap.Logger); ok {
return l
}
return zap.L()
}