mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-25 09:06:34 +08:00
refactor: slog替换logrus、日志操作统一、支持json、text格式等
This commit is contained in:
22
server/pkg/logx/json_handler.go
Normal file
22
server/pkg/logx/json_handler.go
Normal 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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user