-
Notifications
You must be signed in to change notification settings - Fork 5
/
gzap.go
149 lines (124 loc) · 3.67 KB
/
gzap.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gzap
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LevedLogger is a unifying type for all of the exported zap leveled loggers,
// Info, Warn, Error, Debug.
type LevedLogger func(msg string, fields ...zapcore.Field)
// Logger is the global logger for the application. Upon first initalization of
// the logger all calls to 'getLogger' are memoized with the instantiated 'logger'.
var Logger = getLogger()
// logger is the package level pointer to an instantied Logger.
var logger *zap.Logger
var highPriority = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
var lowPriority = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
// InitLogger initializes a global Logger based upon your env configurations.
func InitLogger() error {
return initLogger(&EnvConfig{}, false)
}
func initLogger(cfg Config, disableGraylog bool) error {
// If we're running in a Go test return the test logger.
if cfg.getIsTestEnv() {
return setTestLogger(cfg)
}
// Create a console output enabled zapcore.
zapcore := enableConsoleLogging(cfg)
// Check if Graylog host is defined
// if so return a Graylog Logger with
// console logging enabled.
graylogHost := cfg.getGraylogHost()
if graylogHost != "" && !disableGraylog {
return setGraylogLogger(cfg, zapcore)
}
// Return a console logger by default.
return setLoggerFromCore(zapcore)
}
// getLogger is an internal function that returns an instantied Logger,
// or inits a new logger with default test configuration.
//
// This is because tests that run application code that use the logger will
// need an instaniated Logger to run. In this case we want to make sure we
// use a no-op logger, to reduce test noise.
func getLogger() *zap.Logger {
if logger == nil {
if err := InitLogger(); err != nil {
// If the logger fails to instaniate with it's current configuration
// attempt to initLogger and skip setting up Graylog. This is to prevent
// panicing and shutting down a service when Graylog experiences difficulties.
// If this also fails we have a problem unrelated to Graylog and we need
// to panic as logging has issues that are unrecoverable.
if err != initLogger(&EnvConfig{}, true) {
panic(err)
}
}
}
return logger
}
func setGraylogLogger(cfg Config, consoleLoggingCore zapcore.Core) error {
graylog, err := NewGraylog(cfg)
if err != nil {
return err
}
zapcore := zap.New(
zapcore.NewTee(
NewGelfCore(cfg, graylog),
consoleLoggingCore,
),
zap.AddCaller(),
zap.AddStacktrace(zapcore.ErrorLevel),
zap.Fields(
zapcore.Field{
Key: "env",
String: cfg.getGraylogLogEnvName(),
Type: zapcore.StringType,
},
),
)
logger = zapcore
return nil
}
func enableConsoleLogging(cfg Config) zapcore.Core {
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
encoderConfig := zap.NewDevelopmentEncoderConfig()
// if ENABLE_DATADOG_JSON_FORMATTER then replace
// zapcore console log with zapcore json log
var logEncoder = zapcore.NewConsoleEncoder(encoderConfig)
if cfg.enableJSONFormatter() {
logEncoder = zapcore.NewJSONEncoder(encoderConfig)
}
if cfg.useColoredConsolelogs() {
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
zapcore := zapcore.NewTee(
zapcore.NewCore(
logEncoder,
consoleDebugging,
lowPriority,
),
zapcore.NewCore(
logEncoder,
consoleErrors,
highPriority,
),
)
return zapcore
}
func setTestLogger(cfg Config) error {
zapNopLogger := zap.NewNop()
logger = zapNopLogger
return nil
}
func setLoggerFromCore(core zapcore.Core) error {
logger = zap.New(
core,
zap.AddCaller(),
)
return nil
}