-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.go
60 lines (51 loc) · 1.38 KB
/
logging.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
package main
import (
"bytes"
"log"
"strings"
"go.uber.org/zap"
)
var logger *zap.SugaredLogger
func createLoggerWith(logLevel string) *zap.Logger {
loggerConfig := zap.NewProductionConfig()
loggerConfig.Level = zapLogLevelFrom(logLevel)
loggerConfig.DisableStacktrace = true
logger, e := loggerConfig.Build()
if e != nil {
log.Panic(e)
}
return logger
}
func zapLogLevelFrom(configLogLevel string) zap.AtomicLevel {
switch strings.ToLower(configLogLevel) {
case "", "debug":
return zap.NewAtomicLevelAt(zap.DebugLevel)
case "info":
return zap.NewAtomicLevelAt(zap.InfoLevel)
case "warn":
return zap.NewAtomicLevelAt(zap.WarnLevel)
case "error":
return zap.NewAtomicLevelAt(zap.ErrorLevel)
case "fatal":
return zap.NewAtomicLevelAt(zap.FatalLevel)
default:
log.Fatal("Invalid log level in config", "log-level", configLogLevel)
return zap.NewAtomicLevelAt(-1)
}
}
const (
_stdLogDefaultDepth = 2
_loggerWriterDepth = 1
)
// Copied from go.uber.org/zap/global.go and changed to use Error instead of Info:
func NewStdLog(l *zap.Logger) *log.Logger {
return log.New(&loggerWriter{l.WithOptions(
zap.AddCallerSkip(_stdLogDefaultDepth + _loggerWriterDepth),
)}, "" /* prefix */, 0 /* flags */)
}
type loggerWriter struct{ logger *zap.Logger }
func (l *loggerWriter) Write(p []byte) (int, error) {
p = bytes.TrimSpace(p)
l.logger.Error(string(p))
return len(p), nil
}