You've already forked agentic-coding-workflow
feat(logger): slog-based logger with JSON and text handlers
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
// Package logger 提供基于标准库 log/slog 的结构化日志器封装:
|
||||
// 统一的 Format / Level 选项,以及按环境区分默认值的 FromEnv 便利函数。
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Format 表示日志输出格式。
|
||||
type Format string
|
||||
|
||||
// 支持的输出格式。
|
||||
const (
|
||||
FormatJSON Format = "json"
|
||||
FormatText Format = "text"
|
||||
)
|
||||
|
||||
// Level 是 slog.Level 的类型别名,方便外部直接使用本包常量而无需引入 log/slog。
|
||||
type Level = slog.Level
|
||||
|
||||
// 常用日志级别(与 slog 同名常量等价)。
|
||||
const (
|
||||
LevelDebug = slog.LevelDebug
|
||||
LevelInfo = slog.LevelInfo
|
||||
LevelWarn = slog.LevelWarn
|
||||
LevelError = slog.LevelError
|
||||
)
|
||||
|
||||
// Options 控制日志器的输出格式、最低级别与目标 Writer。
|
||||
// Writer 留空时默认写入 os.Stdout。
|
||||
type Options struct {
|
||||
Format Format
|
||||
Level Level
|
||||
Writer io.Writer
|
||||
}
|
||||
|
||||
// New 按 Options 构造一个 *slog.Logger。Format 非 FormatText 时一律使用 JSON handler。
|
||||
func New(opt Options) *slog.Logger {
|
||||
w := opt.Writer
|
||||
if w == nil {
|
||||
w = os.Stdout
|
||||
}
|
||||
hopts := &slog.HandlerOptions{Level: opt.Level, AddSource: false}
|
||||
var h slog.Handler
|
||||
switch opt.Format {
|
||||
case FormatText:
|
||||
h = slog.NewTextHandler(w, hopts)
|
||||
default:
|
||||
h = slog.NewJSONHandler(w, hopts)
|
||||
}
|
||||
return slog.New(h)
|
||||
}
|
||||
|
||||
// FromEnv 根据环境名返回合适默认:
|
||||
// development → text + debug;其他 → json + info。
|
||||
func FromEnv(env string, w io.Writer) *slog.Logger {
|
||||
if env == "development" {
|
||||
return New(Options{Format: FormatText, Level: LevelDebug, Writer: w})
|
||||
}
|
||||
return New(Options{Format: FormatJSON, Level: LevelInfo, Writer: w})
|
||||
}
|
||||
Reference in New Issue
Block a user