添加运行模式配置,优化调试和生产环境的日志输出
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"github.com/lmittmann/tint"
|
||||
"log/slog"
|
||||
"os"
|
||||
"platform/pkg/env"
|
||||
"time"
|
||||
|
||||
"github.com/lmittmann/tint"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
slog.SetDefault(slog.New(
|
||||
tint.NewHandler(os.Stdout, &tint.Options{
|
||||
|
||||
var writer = os.Stdout
|
||||
var timeFormat = "2006-01-02 15:04:05"
|
||||
|
||||
var handler slog.Handler
|
||||
switch env.RunMode {
|
||||
case "debug":
|
||||
handler = tint.NewHandler(writer, &tint.Options{
|
||||
AddSource: true,
|
||||
Level: env.LogLevel,
|
||||
TimeFormat: time.Kitchen,
|
||||
TimeFormat: timeFormat,
|
||||
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
|
||||
err, ok := attr.Value.Any().(error)
|
||||
if ok {
|
||||
@@ -21,6 +26,19 @@ func Init() {
|
||||
}
|
||||
return attr
|
||||
},
|
||||
}),
|
||||
))
|
||||
})
|
||||
case "production":
|
||||
handler = slog.NewJSONHandler(writer, &slog.HandlerOptions{
|
||||
AddSource: false,
|
||||
Level: env.LogLevel,
|
||||
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == "time" {
|
||||
return slog.String("time", a.Value.Time().Format(timeFormat))
|
||||
}
|
||||
return a
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
slog.SetDefault(slog.New(handler))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user