forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
69 lines (60 loc) · 1.47 KB
/
log.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
package gorillaz
import (
"fmt"
"log"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Log is the main logger.
var Log *zap.Logger
// Sugar is
var Sugar *zap.SugaredLogger
// set default level to INFO
var atomLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
func init() {
config := zap.NewProductionConfig()
err := config.EncoderConfig.EncodeTime.UnmarshalText([]byte("iso8601"))
if err != nil {
log.Fatalf("error trying to define encoding %v", err)
}
config.Level = atomLevel
l, err := config.Build()
if err != nil {
panic(err)
}
Log = l
Sugar = Log.Sugar()
}
// InitLogs initializes the Sugar (*zap.SugaredLogger) and Log (*zap.Logger) elements
// returns an error if logLevel can't be mapped to a zapcore.LogLevel, otherwise returns nil
func (*Gaz) InitLogs(logLevel string) error {
level, err := zapLogLevel(logLevel)
if err != nil {
return err
}
atomLevel.SetLevel(level)
return nil
}
// LogLevel returns the current log level
func (*Gaz) LogLevel() zapcore.Level {
return atomLevel.Level()
}
func zapLogLevel(logLevel string) (zapcore.Level, error) {
switch strings.ToLower(logLevel) {
case "debug":
return zapcore.DebugLevel, nil
case "":
return zapcore.InfoLevel, nil
case "info":
return zapcore.InfoLevel, nil
case "warn":
return zapcore.WarnLevel, nil
case "error":
return zapcore.ErrorLevel, nil
case "panic":
return zapcore.PanicLevel, nil
default:
return zapcore.InfoLevel, fmt.Errorf("invalid error level %s", logLevel)
}
}