-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
logger.go
223 lines (193 loc) · 6.03 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Package logger is used to store details of events in the node.
// Events can be categorized by Trace, Debug, Info, Error, Fatal, and Panic.
package logger
import (
"log"
"reflect"
"runtime"
"gorm.io/gorm"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger is the main interface of this package.
// It implements uber/zap's SugaredLogger interface and adds conditional logging helpers.
type Logger struct {
*zap.SugaredLogger
Orm ORM
lvl zapcore.Level
dir string
jsonConsole bool
toDisk bool
}
// Constants for service names for package specific logging configuration
var (
HeadTracker = "head_tracker"
FluxMonitor = "fluxmonitor"
)
func GetLogServices() []string {
return []string{HeadTracker, FluxMonitor}
}
// Write logs a message at the Info level and returns the length
// of the given bytes.
func (l *Logger) Write(b []byte) (int, error) {
l.Info(string(b))
return len(b), nil
}
// WarnIf logs the error if present.
func (l *Logger) WarnIf(err error) {
if err != nil {
l.Warn(err)
}
}
// ErrorIf logs the error if present.
func (l *Logger) ErrorIf(err error, optionalMsg ...string) {
if err != nil {
if len(optionalMsg) > 0 {
l.Error(errors.Wrap(err, optionalMsg[0]))
} else {
l.Error(err)
}
}
}
// ErrorIfCalling calls the given function and logs the error of it if there is.
func (l *Logger) ErrorIfCalling(f func() error, optionalMsg ...string) {
err := f()
if err != nil {
e := errors.Wrap(err, runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
if len(optionalMsg) > 0 {
Default.Error(errors.Wrap(e, optionalMsg[0]))
} else {
Default.Error(e)
}
}
}
func (l *Logger) PanicIf(err error) {
if err != nil {
l.Panic(err)
}
}
func (l *Logger) SetDB(db *gorm.DB) {
l.Orm = NewORM(db)
}
// GetServiceLogLevels retrieves all service log levels from the db
func (l *Logger) GetServiceLogLevels() (map[string]string, error) {
serviceLogLevels := make(map[string]string)
headTracker, err := l.ServiceLogLevel(HeadTracker)
if err != nil {
Fatalf("error getting service log levels: %v", err)
}
serviceLogLevels[HeadTracker] = headTracker
fluxMonitor, err := l.ServiceLogLevel(FluxMonitor)
if err != nil {
Fatalf("error getting service log levels: %v", err)
}
serviceLogLevels[FluxMonitor] = fluxMonitor
return serviceLogLevels, nil
}
// CreateLogger dwisott
func CreateLogger(zl *zap.SugaredLogger) *Logger {
return &Logger{SugaredLogger: zl}
}
func CreateLoggerWithConfig(zl *zap.SugaredLogger, lvl zapcore.Level, dir string, jsonConsole bool, toDisk bool) *Logger {
return &Logger{
SugaredLogger: zl,
lvl: lvl,
dir: dir,
jsonConsole: jsonConsole,
toDisk: toDisk,
}
}
// initLogConfig builds a zap.Config for a logger
func initLogConfig(dir string, jsonConsole bool, lvl zapcore.Level, toDisk bool) zap.Config {
config := zap.NewProductionConfig()
if !jsonConsole {
config.OutputPaths = []string{"pretty://console"}
}
if toDisk {
destination := logFileURI(dir)
config.OutputPaths = append(config.OutputPaths, destination)
config.ErrorOutputPaths = append(config.ErrorOutputPaths, destination)
}
config.Level.SetLevel(lvl)
return config
}
// CreateProductionLogger returns a log config for the passed directory
// with the given LogLevel and customizes stdout for pretty printing.
func CreateProductionLogger(
dir string, jsonConsole bool, lvl zapcore.Level, toDisk bool) *Logger {
config := initLogConfig(dir, jsonConsole, lvl, toDisk)
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}
return CreateLoggerWithConfig(zl.Sugar(), lvl, dir, jsonConsole, toDisk)
}
// InitServiceLevelLogger builds a service level logger with a given logging level & serviceName
func (l *Logger) InitServiceLevelLogger(serviceName string, logLevel string) (*Logger, error) {
var ll zapcore.Level
if err := ll.UnmarshalText([]byte(logLevel)); err != nil {
return nil, err
}
config := initLogConfig(l.dir, l.jsonConsole, ll, l.toDisk)
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
return nil, err
}
return CreateLoggerWithConfig(zl.Named(serviceName).Sugar(), ll, l.dir, l.jsonConsole, l.toDisk), nil
}
// ServiceLogLevel is the log level set for a specified package
func (l *Logger) ServiceLogLevel(serviceName string) (string, error) {
if l.Orm != nil {
level, err := l.Orm.GetServiceLogLevel(serviceName)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
Warnf("Error while trying to fetch %s service log level: %v", serviceName, err)
} else if err == nil {
return level, nil
}
}
return l.lvl.String(), nil
}
// NewProductionConfig returns a production logging config
func NewProductionConfig(lvl zapcore.Level, dir string, jsonConsole, toDisk bool) (c zap.Config) {
var outputPath string
if jsonConsole {
outputPath = "stderr"
} else {
outputPath = "pretty://console"
}
// Mostly copied from zap.NewProductionConfig with sampling disabled
c = zap.Config{
Level: zap.NewAtomicLevelAt(lvl),
Development: false,
Sampling: nil,
Encoding: "json",
EncoderConfig: NewProductionEncoderConfig(),
OutputPaths: []string{outputPath},
ErrorOutputPaths: []string{"stderr"},
}
if toDisk {
destination := logFileURI(dir)
c.OutputPaths = append(c.OutputPaths, destination)
c.ErrorOutputPaths = append(c.ErrorOutputPaths, destination)
}
return
}
// NewProductionEncoderConfig returns a production encoder config
func NewProductionEncoderConfig() zapcore.EncoderConfig {
// Copied from zap.NewProductionEncoderConfig but with ISO timestamps instead of Unix
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}