-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
100 lines (89 loc) · 2.18 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package zapvml
import (
"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/kelseyhightower/envconfig"
"google.golang.org/grpc/codes"
)
var (
// Log is global logger
Log *zap.Logger
Level zap.AtomicLevel
)
type Config struct {
Level zapcore.Level `required:"true" default:"warn"`
Debug bool `required:"true" default:"false"`
ServiceName string `required:"true" default:"default_service"`
}
func Init(globalLevel zapcore.Level) {
Level.SetLevel(globalLevel)
}
// Use package init to avoid race conditions for GRPC options
// sync.Once still suffers from races, init functions are less complex than sync.once + waitgroup
func init() {
var cfg Config
if err := envconfig.Process("log", &cfg); err != nil {
panic(err)
}
err := zap.RegisterEncoder("stackdriver-json", newEncoder)
if err != nil {
panic(err)
}
var config zap.Config
if cfg.Debug {
config = zapdriver.NewDevelopmentConfig()
config.Encoding = "console"
} else {
config = zapdriver.NewProductionConfig()
config.Encoding = "stackdriver-json"
}
Log, err = config.Build(zapdriver.WrapCore(
zapdriver.ReportAllErrors(true),
zapdriver.ServiceName(cfg.ServiceName),
))
if err != nil {
panic(err)
}
zap.RedirectStdLog(Log)
}
func CodeToLevel(code codes.Code) zapcore.Level {
switch code {
case codes.OK:
return zap.InfoLevel
case codes.Canceled:
return zap.WarnLevel
case codes.Unknown:
return zap.ErrorLevel
case codes.InvalidArgument:
return zap.WarnLevel
case codes.DeadlineExceeded:
return zap.WarnLevel
case codes.NotFound:
return zap.WarnLevel
case codes.AlreadyExists:
return zap.WarnLevel
case codes.PermissionDenied:
return zap.WarnLevel
case codes.Unauthenticated:
return zap.WarnLevel
case codes.ResourceExhausted:
return zap.WarnLevel
case codes.FailedPrecondition:
return zap.WarnLevel
case codes.Aborted:
return zap.WarnLevel
case codes.OutOfRange:
return zap.WarnLevel
case codes.Unimplemented:
return zap.ErrorLevel
case codes.Internal:
return zap.ErrorLevel
case codes.Unavailable:
return zap.WarnLevel
case codes.DataLoss:
return zap.ErrorLevel
default:
return zap.ErrorLevel
}
}