-
Notifications
You must be signed in to change notification settings - Fork 3
/
zap.go
50 lines (40 loc) · 947 Bytes
/
zap.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
42
43
44
45
46
47
48
49
50
package zap
import (
"context"
"github.com/alexfalkowski/go-service/env"
"github.com/alexfalkowski/go-service/os"
"github.com/alexfalkowski/go-service/version"
"go.uber.org/fx"
"go.uber.org/zap"
)
// LoggerParams for zap.
type LoggerParams struct {
fx.In
Lifecycle fx.Lifecycle
Config *Config
ZapConfig zap.Config
Environment env.Environment
Version version.Version
}
// NewLogger using zap.
func NewLogger(params LoggerParams) (*zap.Logger, error) {
if !params.Config.Enabled {
return zap.NewNop(), nil
}
fields := zap.Fields(
zap.String("name", os.ExecutableName()),
zap.String("environment", string(params.Environment)),
zap.String("version", string(params.Version)),
)
logger, err := params.ZapConfig.Build(fields)
if err != nil {
return nil, err
}
params.Lifecycle.Append(fx.Hook{
OnStop: func(_ context.Context) error {
_ = logger.Sync()
return nil
},
})
return logger, nil
}