Documentation
¶
Overview ¶
Package kit 提供基于标准库 slog 的日志封装,支持上下文提取、webhook 通知和堆栈跟踪。
Index ¶
- func AddWebhook(config *WebhookConfig)
- func Debug(ctx context.Context, msg string, fields ...any)
- func Debugf(ctx context.Context, format string, args ...any)
- func Error(ctx context.Context, msg string, fields ...any)
- func Errorf(ctx context.Context, format string, args ...any)
- func Fatal(ctx context.Context, msg string, fields ...any)
- func Fatalf(ctx context.Context, format string, args ...any)
- func Info(ctx context.Context, msg string, fields ...any)
- func Infof(ctx context.Context, format string, args ...any)
- func Init(opts Options)
- func Panic(ctx context.Context, msg string, fields ...any)
- func Panicf(ctx context.Context, format string, args ...any)
- func SetDefaultContextKeys(keys ContextKeys)
- func SetLevel(level string)
- func Warn(ctx context.Context, msg string, fields ...any)
- func Warnf(ctx context.Context, format string, args ...any)
- func WithCtx(ctx context.Context) *contextLogger
- type ContextKeys
- type Format
- type Level
- type LogRecord
- type Logger
- func (l *Logger) Debug(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Debugf(ctx context.Context, format string, args ...any)
- func (l *Logger) Error(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Errorf(ctx context.Context, format string, args ...any)
- func (l *Logger) Fatal(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Fatalf(ctx context.Context, format string, args ...any)
- func (l *Logger) Info(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Infof(ctx context.Context, format string, args ...any)
- func (l *Logger) Panic(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Panicf(ctx context.Context, format string, args ...any)
- func (l *Logger) SetLevel(level string)
- func (l *Logger) Sync() error
- func (l *Logger) Warn(ctx context.Context, msg string, fields ...any)
- func (l *Logger) Warnf(ctx context.Context, format string, args ...any)
- func (l *Logger) WithCtx(ctx context.Context) *contextLogger
- func (l *Logger) Write(p []byte) (n int, err error)
- type Options
- type StackTraceConfig
- type WebhookBuildPayload
- type WebhookConfig
- type WebhookFilter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultContextKeys ¶
func SetDefaultContextKeys(keys ContextKeys)
SetDefaultContextKeys 设置全局默认 context keys
Types ¶
type ContextKeys ¶
type ContextKeys struct {
// Trace trace ID 可能的 key 列表,按优先级查找
Trace []string
// Request request ID 可能的 key 列表,按优先级查找
Request []string
// User user ID 可能的 key 列表,按优先级查找
User []string
// Custom 自定义字段映射,key 为日志字段名,value 为可能的 context key 列表
// 例如:{"session_id": ["session_id", "sid", "x-session-id"]}
Custom map[string][]string
}
ContextKeys 定义要从 context 中提取的 keys
type Level ¶
type Level int
Level 日志级别
func ParseLevel ¶
ParseLevel 从字符串解析日志级别 输入字符串会转成大写后匹配,如 "info", "INFO", "Info" 都能正确解析
type LogRecord ¶
type LogRecord struct {
Level Level
Message string
Time time.Time
TraceID string
RequestID string
UserID string
Caller string
StackTrace string
Fields map[string]interface{}
}
LogRecord 日志记录信息,用于 webhook
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger 日志记录器
func (*Logger) SetLevel ¶
SetLevel 设置日志级别 level 可以是: "debug", "info", "warn", "error", "fatal", "panic"
type Options ¶
type Options struct {
// Level 日志级别,默认 "info"
// 支持: "debug", "info", "warn", "error", "fatal", "panic"
Level string
// Format 输出格式,默认 FormatJSON
Format Format
// Output 输出目标,默认 os.Stdout
// 可以通过 io.MultiWriter(os.Stdout, file) 同时输出到多个目标
Output io.Writer
// AddCaller 是否添加调用者信息(文件:行号),默认 true
AddCaller bool
// StackTrace 堆栈跟踪配置
StackTrace StackTraceConfig
// ContextKeys 上下文提取配置,如果不设置使用全局默认值
ContextKeys *ContextKeys
// Webhooks webhook 配置列表,错误级别自动触发
Webhooks []*WebhookConfig
// TimeFormat 时间格式,默认 time.RFC3339
TimeFormat string
// Color 是否启用颜色输出(仅文本格式),默认 false
Color bool
}
Options 日志配置选项
type StackTraceConfig ¶
type StackTraceConfig struct {
// Enabled 是否启用堆栈跟踪
Enabled bool
// Level 触发堆栈的最小级别,默认 Error
Level Level
// Depth 堆栈深度,默认 32
Depth int
// SkipRuntime 是否跳过 runtime 帧,默认 true
SkipRuntime bool
}
StackTraceConfig 堆栈跟踪配置
type WebhookBuildPayload ¶
WebhookBuildPayload 自定义 payload 构建函数类型
type WebhookConfig ¶
type WebhookConfig struct {
// Name webhook 名称,用于标识
Name string
// URL 接收地址(必填)
URL string
// Method 请求方法,默认 POST
Method string
// Headers 额外请求头
Headers map[string]string
// BuildPayload 自定义 payload 构建函数
// 如果为空,使用默认 payload 格式
BuildPayload WebhookBuildPayload
// Filter 过滤函数,只有返回 true 才发送
// 如果为空,默认只发送 Error 及以上级别
Filter WebhookFilter
// Timeout 请求超时,默认 5 秒
Timeout time.Duration
}
WebhookConfig Webhook 配置
Click to show internal directories.
Click to hide internal directories.