-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
68 lines (59 loc) · 1.35 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
package service
import (
"encoding/json"
"log"
"os"
"time"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger ...
var Logger *zap.Logger
// InitLogger ...
func InitLogger() {
lp := Conf.Common.LogPath
lv := Conf.Common.LogLevel
isDebug := true
if Conf.Common.IsDebug != true {
isDebug = false
}
initLogger(lp, lv, isDebug)
log.SetFlags(log.Lmicroseconds | log.Lshortfile | log.LstdFlags)
go func() {
for {
time.Sleep(5 * time.Second)
checkLog(lp, lv, isDebug)
}
}()
}
func fileExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
func checkLog(lp string, lv string, isDebug bool) {
if !fileExist(lp) {
initLogger(lp, lv, isDebug)
log.SetFlags(log.Lmicroseconds | log.Lshortfile | log.LstdFlags)
}
}
func initLogger(lp string, lv string, isDebug bool) {
js := fmt.Sprintf(`{
"level": "%s",
"encoding": "json",
"outputPaths": ["stdout","%s"],
"errorOutputPaths": ["stderr","%s"]
}`, lv, lp, lp)
var cfg zap.Config
if err := json.Unmarshal([]byte(js), &cfg); err != nil {
panic(err)
}
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoderScc
// cfg.EncoderConfig.EncodeCaller = zapcore.FullCallerEncoder
var err error
Logger, err = cfg.Build()
if err != nil {
log.Fatal("init logger error: ", err)
}
}