refactor: slog替换logrus、日志操作统一、支持json、text格式等

This commit is contained in:
meilin.huang
2023-09-02 17:24:18 +08:00
parent d51cd4b289
commit 899a3a8243
47 changed files with 685 additions and 293 deletions

View File

@@ -0,0 +1,22 @@
package logx
import (
"log/slog"
"time"
)
func NewJsonHandler(config *Config) *slog.JSONHandler {
replace := func(groups []string, a slog.Attr) slog.Attr {
// 格式化时间.
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{Key: "time", Value: slog.StringValue(time.Now().Local().Format("2006-01-02 15:04:05.000"))}
}
return a
}
return slog.NewJSONHandler(config.GetLogOut(), &slog.HandlerOptions{
Level: config.GetLevel(),
AddSource: false, // 统一由添加公共commonAttrs时判断添加
ReplaceAttr: replace,
})
}