首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 初始化slog:JSON格式输出,INFO级别,自定义时间格式
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
||||
// 将默认ISO8601时间改为“年-月-日 时:分:秒”
|
||||
if a.Key == slog.TimeKey {
|
||||
a.Value = slog.StringValue(a.Value.Time().Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
return a
|
||||
},
|
||||
}))
|
||||
|
||||
// 替代 fmt.Printf("[2025-10-22 16:00:00] [INFO] 用户1001登录,耗时35ms\n")
|
||||
logger.Info("用户登录成功",
|
||||
"user_id", 1001,
|
||||
"cost_ms", 35,
|
||||
"ip", "192.168.1.100")
|
||||
}
|
||||
Reference in New Issue
Block a user