-
Notifications
You must be signed in to change notification settings - Fork 8
/
log.go
38 lines (35 loc) · 939 Bytes
/
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
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"os"
"path/filepath"
)
var logger *zap.SugaredLogger
func InitLogger(level string) {
writeSyncer := getLogWriter(level)
encoder := getEncoder()
core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
l := zap.New(core, zap.AddCaller())
logger = l.Sugar()
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}
func getLogWriter(level string) zapcore.WriteSyncer {
ws := io.MultiWriter(os.Stdout)
if level == "1" {
ex, err := os.Executable()
if err != nil {
logger.Panic(err)
}
exPath := filepath.Dir(ex)
file, _ := os.Create(filepath.Join(exPath, "debug.log"))
ws = io.MultiWriter(file, os.Stdout)
}
return zapcore.AddSync(ws)
}