/
logger_utils.go
67 lines (60 loc) · 1.55 KB
/
logger_utils.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
package util
import (
"fmt"
"os"
"path/filepath"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func InitLogger(filePath string) *zap.Logger {
dirLogFile, fileLogFile := filepath.Split("log/" + filePath)
logFilePath := dirLogFile + fileLogFile
consoleConfig := zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Name",
CallerKey: "Caller",
MessageKey: "Msg",
StacktraceKey: "St",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
jsonConfig := zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Name",
CallerKey: "Caller",
MessageKey: "Msg",
StacktraceKey: "St",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
err := Check_dir(logFilePath)
if err != nil {
fmt.Printf("Directory check exist error: %s\n", err)
return nil
}
file, err := os.Create(logFilePath)
if err != nil {
fmt.Printf("Create file error: %s", err)
}
consoleCore := zapcore.NewCore(
zapcore.NewConsoleEncoder(consoleConfig),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
)
logCore := zapcore.NewCore(
zapcore.NewJSONEncoder(jsonConfig),
zapcore.AddSync(file),
zapcore.InfoLevel,
)
logger := zap.New(zapcore.NewTee(
consoleCore,
logCore,
))
return logger
}