/
gwlog.go
229 lines (194 loc) · 5.49 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
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
224
225
226
227
228
229
package gwlog
import (
"os"
"runtime/debug"
"strings"
"encoding/json"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
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
filename string
logStd bool
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()
}
// SetRotateFile sets the output writer
func SetRotateFile(logfile string, logstd bool) {
filename = logfile
logStd = logstd
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() {
syncWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: filename,
MaxSize: 128, //MB //1 << 30, //1G
LocalTime: true,
Compress: true,
})
logPriority := zap.NewAtomicLevelAt(currentLevel)
var allCore []zapcore.Core
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
if logStd {
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleConfig.EncodeTime = zapcore.ISO8601TimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
consoleDebugging := zapcore.Lock(os.Stderr) //zapcore.Lock(os.Stdout)
allCore = append(allCore, zapcore.NewCore(consoleEncoder, consoleDebugging, logPriority))
}
allCore = append(allCore, zapcore.NewCore(jsonEncoder, syncWriter, logPriority))
core := zapcore.NewTee(allCore...)
logger := zap.New(core).WithOptions(zap.AddCaller(), zap.AddCallerSkip(1))
if source != "" {
// logger = logger.With(zap.String("source", source))
logger = logger.WithOptions(zap.Fields(zap.String("source", source)))
}
setSugar(logger.Sugar())
// 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...)
sugar.Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Infof(format, args...)
sugar.Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Warnf(format, args...)
sugar.Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Errorf(format, args...)
sugar.Errorf(format, args...)
}
func Panicf(format string, args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Panicf(format, args...)
sugar.Panicf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
debug.PrintStack()
// sugar.With(zap.Time("ts", time.Now())).Fatalf(format, args...)
sugar.Fatalf(format, args...)
}
func Debug(args ...interface{}) {
sugar.Debug(args...)
}
func Info(args ...interface{}) {
sugar.Info(args...)
}
func Warn(args ...interface{}) {
sugar.Warn(args...)
}
func Error(args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Error(args...)
sugar.Error(args...)
}
func Panic(args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Panic(args...)
sugar.Panic(args...)
}
func Fatal(args ...interface{}) {
// sugar.With(zap.Time("ts", time.Now())).Fatal(args...)
sugar.Fatal(args...)
}
func setSugar(sugar_ *zap.SugaredLogger) {
sugar = sugar_
}