/
logger.go
168 lines (143 loc) · 4.17 KB
/
logger.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
package glog
/*
@File: logger.go
@Author: khaosles
@Time: 2023/4/11 22:16
@Desc:
*/
import (
"log"
"os"
"path/filepath"
"time"
"github.com/khaosles/giz/fileutil"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/khaosles/go-contrib/core/config"
)
const (
ONE_DAY = time.Hour * 24
APP = "logging"
)
var prefix string
var Logger *zap.SugaredLogger
type logging struct {
LevelConsole string `mapstructure:"level-console" default:"debug" yaml:"level-console" json:"level-console"`
LevelFile string `mapstructure:"level-file" default:"info" yaml:"level-file" json:"level-file"`
Prefix string `mapstructure:"prefix" default:"" yaml:"prefix" json:"prefix"`
Path string `mapstructure:"path" default:"" yaml:"path" json:"path"`
MaxHistory time.Duration `mapstructure:"max-history" default:"7" yaml:"max-history" json:"maxHistory"`
LogInConsole bool `mapstructure:"log-in-console" default:"true" yaml:"log-in-console" json:"logInConsole"`
LogInFile bool `mapstructure:"log-in-file" default:"false" yaml:"log-in-file" json:"logInFile"`
ShowLine bool `mapstructure:"show-line" default:"false" yaml:"show-line" json:"show-line"`
}
func init() {
var logging logging
if err := config.Configuration(APP, &logging); err != nil {
log.Fatal(err)
}
prefix = logging.Prefix
encoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
NameKey: "logger",
CallerKey: "caller",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: encodeTime,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
})
levelConsole := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= levelChoice(logging.LevelConsole)
})
levelFile := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= levelChoice(logging.LevelFile)
})
var cores []zapcore.Core
if logging.LogInFile {
path := logPath(logging.Path)
hook, _ := rotatelogs.New(
path+"/%Y-%m-%d.log",
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(ONE_DAY*logging.MaxHistory),
rotatelogs.WithRotationTime(ONE_DAY),
rotatelogs.WithClock(rotatelogs.Local),
)
cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(hook), levelFile))
if logging.LogInConsole {
cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), levelConsole))
}
} else {
cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), levelConsole))
}
cores = append(cores)
core := zapcore.NewTee(cores...)
l := zap.New(core)
if logging.ShowLine {
l = l.WithOptions(zap.AddCaller(), zap.AddCallerSkip(1))
}
Logger = l.Sugar()
}
func logPath(path string) string {
if !filepath.IsAbs(path) {
wk, _ := os.Getwd()
fileutil.Join(wk, path)
}
_ = fileutil.Mkdir(path)
return path
}
func encodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(prefix + t.In(time.FixedZone("CTS", 8*3600)).Format("2006-01-02 15:04:05.000"))
}
func levelChoice(level string) zapcore.Level {
switch level {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
case "panic":
return zapcore.PanicLevel
default:
return zapcore.InfoLevel
}
}
func Debug(args ...interface{}) {
Logger.Debug(args...)
}
func Debugf(fmt string, args ...interface{}) {
Logger.Debugf(fmt, args...)
}
func Info(args ...interface{}) {
Logger.Info(args...)
}
func Infof(fmt string, args ...interface{}) {
Logger.Infof(fmt, args...)
}
func Warn(args ...interface{}) {
Logger.Warn(args...)
}
func Warnf(fmt string, args ...interface{}) {
Logger.Warnf(fmt, args...)
}
func Error(args ...interface{}) {
Logger.Error(args...)
}
func Errorf(fmt string, args ...interface{}) {
Logger.Errorf(fmt, args...)
}
func Panic(args ...interface{}) {
Logger.Panic(args...)
}
func Panicf(fmt string, args ...interface{}) {
Logger.Panicf(fmt, args...)
}
func Fatal(args ...interface{}) {
Logger.Panic(args...)
os.Exit(1)
}