forked from xiaonanln/goworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwlog.go
169 lines (141 loc) · 3.68 KB
/
gwlog.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
package gwlog
import (
"runtime/debug"
"strings"
"encoding/json"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
// DebugLevel level
DebugLevel Level = Level(zap.DebugLevel)
// InfoLevel level
InfoLevel Level = Level(zap.InfoLevel)
// WarnLevel level
WarnLevel Level = Level(zap.WarnLevel)
// ErrorLevel level
ErrorLevel Level = Level(zap.ErrorLevel)
// PanicLevel level
PanicLevel Level = Level(zap.PanicLevel)
// FatalLevel level
FatalLevel Level = Level(zap.FatalLevel)
)
type logFormatFunc func(format string, args ...interface{})
// Level is type of log levels
type Level = zapcore.Level
var (
cfg zap.Config
logger *zap.Logger
sugar *zap.SugaredLogger
source string
currentLevel Level
)
func init() {
var err error
cfgJson := []byte(`{
"level": "debug",
"outputPaths": ["stderr"],
"errorOutputPaths": ["stderr"],
"encoding": "console",
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
currentLevel = DebugLevel
if err = json.Unmarshal(cfgJson, &cfg); err != nil {
panic(err)
}
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
rebuildLoggerFromCfg()
}
// SetSource sets the component name (dispatcher/gate/game) of gwlog module
func SetSource(source_ string) {
source = source_
rebuildLoggerFromCfg()
}
// SetLevel sets the log level
func SetLevel(lv Level) {
currentLevel = lv
cfg.Level.SetLevel(lv)
}
// GetLevel get the current log level
func GetLevel() Level {
return currentLevel
}
// TraceError prints the stack and error
func TraceError(format string, args ...interface{}) {
Error(string(debug.Stack()))
Errorf(format, args...)
}
// SetOutput sets the output writer
func SetOutput(outputs []string) {
cfg.OutputPaths = outputs
rebuildLoggerFromCfg()
}
// ParseLevel converts string to Levels
func ParseLevel(s string) Level {
if strings.ToLower(s) == "debug" {
return DebugLevel
} else if strings.ToLower(s) == "info" {
return InfoLevel
} else if strings.ToLower(s) == "warn" || strings.ToLower(s) == "warning" {
return WarnLevel
} else if strings.ToLower(s) == "error" {
return ErrorLevel
} else if strings.ToLower(s) == "panic" {
return PanicLevel
} else if strings.ToLower(s) == "fatal" {
return FatalLevel
}
Errorf("ParseLevel: unknown level: %s", s)
return DebugLevel
}
func rebuildLoggerFromCfg() {
if newLogger, err := cfg.Build(); err == nil {
if logger != nil {
logger.Sync()
}
logger = newLogger
//logger = logger.With(zap.Time("ts", time.Now()))
if source != "" {
logger = logger.With(zap.String("source", source))
}
setSugar(logger.Sugar())
} else {
panic(err)
}
}
func Debugf(format string, args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Errorf(format, args...)
}
func Panicf(format string, args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Panicf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
debug.PrintStack()
sugar.With(zap.Time("ts", time.Now())).Fatalf(format, args...)
}
func Error(args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Error(args...)
}
func Panic(args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Panic(args...)
}
func Fatal(args ...interface{}) {
sugar.With(zap.Time("ts", time.Now())).Fatal(args...)
}
func setSugar(sugar_ *zap.SugaredLogger) {
sugar = sugar_
}