Documentation
¶
Overview ¶
Package log 提供日志管理功能,用于 enhance 框架。
该模块提供统一的日志抽象接口,支持多种日志后端集成。 包含日志构建器、上下文日志、slog 集成等日志记录支持。
架构设计 ¶
- Logger: 日志接口,定义统一的日志操作
- LoggerWithLevel: 支持自定义日志级别
- LoggerWithName: 支持日志命名
- LoggerWithCaller: 支持调用者信息
- LoggerWithTimeout: 支持超时日志
- Level: 日志级别枚举
- KeyValue: 日志键值对
- LoggerOption: 日志构建选项
核心功能 ¶
- 统一接口: 提供统一的日志抽象接口
- 多后端: 支持 zap、zerolog、slog 等多种日志后端
- 上下文日志: 支持请求链路追踪和上下文传递
- 日志级别: 支持 DEBUG、INFO、WARN、ERROR 等级别
使用方式 ¶
使用默认日志器:
logger := log.Build() logger.Info(context.Background(), "Application started")
使用上下文日志:
ctxLogger := log.FromContext(ctx)
ctxLogger.Info(context.Background(), "Processing request", log.KeyValue{Key: "request_id", Value: reqID})
集成后端 ¶
具体实现位于 starter 子包:
- starter/zap: Uber Zap 集成
- starter/zerolog: Zerolog 集成
- log/slog: Go 标准库 slog 集成
Package log 提供日志管理功能。
Package log 提供日志管理功能,用于 enhance 框架。
Index ¶
- Variables
- func GetTraceID(ctx context.Context) string
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type ContextLogger
- func (l *ContextLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) Sync() error
- func (l *ContextLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)
- func (l *ContextLogger) With(ctx context.Context, keys ...KeyValue) Logger
- type DynamicLevelLogger
- func (d *DynamicLevelLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) GetLevel() Level
- func (d *DynamicLevelLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) SetLevel(level Level)
- func (d *DynamicLevelLogger) Sync() error
- func (d *DynamicLevelLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)
- func (d *DynamicLevelLogger) With(ctx context.Context, keys ...KeyValue) Logger
- type KeyValue
- type Level
- type Logger
- type LoggerBuilder
- func (b *LoggerBuilder) AddSource(addSource bool) *LoggerBuilder
- func (b *LoggerBuilder) Build() Logger
- func (b *LoggerBuilder) Format(format string) *LoggerBuilder
- func (b *LoggerBuilder) Level(level Level) *LoggerBuilder
- func (b *LoggerBuilder) Name(name string) *LoggerBuilder
- func (b *LoggerBuilder) OutputPath(path string) *LoggerBuilder
- func (b *LoggerBuilder) Sampler(sampler Sampler) *LoggerBuilder
- type LoggerFatal
- type LoggerOption
- type LoggerWithCaller
- type LoggerWithLevel
- type LoggerWithName
- type LoggerWithTimeout
- type Option
- type RandomSampler
- type SampledLogger
- func (l *SampledLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) Sync() error
- func (l *SampledLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SampledLogger) With(ctx context.Context, keys ...KeyValue) Logger
- type Sampler
- type SlogLogger
- func (l *SlogLogger) Close() error
- func (l *SlogLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) Sync() error
- func (l *SlogLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)
- func (l *SlogLogger) With(ctx context.Context, keys ...KeyValue) Logger
- type ThresholdSampler
Constants ¶
This section is empty.
Variables ¶
var TraceContextKey = contextKey{}
TraceContextKey 追踪 ID 上下文键
Functions ¶
Types ¶
type ContextLogger ¶
type ContextLogger struct {
// contains filtered or unexported fields
}
ContextLogger 上下文感知日志器
自动从 ctx 中提取 trace_id 等信息并添加到日志中。
func NewContextLogger ¶
func NewContextLogger(logger Logger) *ContextLogger
NewContextLogger 创建上下文感知日志器
参数:
- logger: 底层日志器
返回:
- *ContextLogger: 上下文日志器实例
func (*ContextLogger) DPanic ¶
func (l *ContextLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
DPanic 记录致命错误日志并 panic
func (*ContextLogger) Debug ¶
func (l *ContextLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
Debug 记录调试日志
func (*ContextLogger) Error ¶
func (l *ContextLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
Error 记录错误日志
func (*ContextLogger) Fatal ¶
func (l *ContextLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
Fatal 记录致命级别日志
func (*ContextLogger) Info ¶
func (l *ContextLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
Info 记录信息日志
func (*ContextLogger) Panic ¶
func (l *ContextLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
Panic 记录日志并 panic
type DynamicLevelLogger ¶
type DynamicLevelLogger struct {
// contains filtered or unexported fields
}
DynamicLevelLogger 动态级别日志器
支持运行时动态调整日志级别,无需重启服务。
func NewDynamicLevelLogger ¶
func NewDynamicLevelLogger(logger Logger, initialLevel Level) *DynamicLevelLogger
NewDynamicLevelLogger 创建动态级别日志器
参数:
- logger: 底层日志器
- initialLevel: 初始日志级别
返回:
- *DynamicLevelLogger: 动态级别日志器实例
func (*DynamicLevelLogger) DPanic ¶
func (d *DynamicLevelLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
DPanic 记录致命错误日志并 panic
func (*DynamicLevelLogger) Debug ¶
func (d *DynamicLevelLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
Debug 记录调试日志
func (*DynamicLevelLogger) Error ¶
func (d *DynamicLevelLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
Error 记录错误日志
func (*DynamicLevelLogger) Fatal ¶
func (d *DynamicLevelLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
Fatal 记录致命级别日志
func (*DynamicLevelLogger) GetLevel ¶
func (d *DynamicLevelLogger) GetLevel() Level
GetLevel 获取当前日志级别
返回:
- Level: 当前日志级别
func (*DynamicLevelLogger) Info ¶
func (d *DynamicLevelLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
Info 记录信息日志
func (*DynamicLevelLogger) Panic ¶
func (d *DynamicLevelLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
Panic 记录日志并 panic
func (*DynamicLevelLogger) SetLevel ¶
func (d *DynamicLevelLogger) SetLevel(level Level)
SetLevel 动态设置日志级别
参数:
- level: 新的日志级别
type Level ¶
type Level int8
Level 定义日志级别。
日志级别从低到高,用于控制日志输出的详细程度。 生产环境推荐使用 InfoLevel 或 WarnLevel。
级别说明 ¶
- DebugLevel: 调试级别,详细的调试信息,生产环境通常禁用
- InfoLevel: 信息级别,一般运行信息,如启动/关闭
- WarnLevel: 警告级别,潜在问题,但不影响正常运行
- ErrorLevel: 错误级别,错误信息,需要关注但不影响核心功能
- DPanicLevel: 致命错误级别,开发环境 panic,生产环境仅记录错误
- PanicLevel: panic 级别,记录日志后 panic
- FatalLevel: 致命级别,记录日志后退出程序
type Logger ¶
type Logger interface {
// Debug 记录调试日志。
Debug(ctx context.Context, msg string, keys ...KeyValue)
// Info 记录信息日志。
Info(ctx context.Context, msg string, keys ...KeyValue)
// Warn 记录警告日志。
Warn(ctx context.Context, msg string, keys ...KeyValue)
// Error 记录错误日志。
Error(ctx context.Context, msg string, keys ...KeyValue)
// Sync 同步日志缓冲区,确保日志写入完成。
Sync() error
// With 返回带有额外字段的日志记录器。
With(ctx context.Context, keys ...KeyValue) Logger
}
Logger 是日志记录器接口,所有日志库都需实现此接口。
提供统一的日志记录 API,支持结构化日志和上下文传递。 实现可以基于 slog、zap、zerolog 等日志库。
使用示例 ¶
logger := log.Build()
logger.Info(context.Background(), "Server started",
log.KeyValue{Key: "port", Value: 8080})
// 带额外字段的日志
child := logger.With(context.Background(), log.KeyValue{Key: "module", Value: "auth"})
child.Info(context.Background(), "User login")
func Build ¶
func Build(opts ...LoggerOption) Logger
Build 使用选项构建日志记录器。 未指定 WithLogger 时默认创建基于 slog 的日志器。
type LoggerBuilder ¶
type LoggerBuilder struct {
// contains filtered or unexported fields
}
LoggerBuilder 日志器构建器
func (*LoggerBuilder) AddSource ¶
func (b *LoggerBuilder) AddSource(addSource bool) *LoggerBuilder
AddSource 设置是否添加源码位置
func (*LoggerBuilder) Format ¶
func (b *LoggerBuilder) Format(format string) *LoggerBuilder
Format 设置输出格式(json 或 text)
func (*LoggerBuilder) Level ¶
func (b *LoggerBuilder) Level(level Level) *LoggerBuilder
Level 设置日志级别
func (*LoggerBuilder) OutputPath ¶
func (b *LoggerBuilder) OutputPath(path string) *LoggerBuilder
OutputPath 设置日志文件输出路径
func (*LoggerBuilder) Sampler ¶
func (b *LoggerBuilder) Sampler(sampler Sampler) *LoggerBuilder
Sampler 设置采样策略
type LoggerFatal ¶ added in v0.0.3
type LoggerFatal interface {
Logger
// DPanic 记录致命错误日志并在开发环境 panic。
DPanic(ctx context.Context, msg string, keys ...KeyValue)
// Panic 记录日志并 panic。
Panic(ctx context.Context, msg string, keys ...KeyValue)
// Fatal 记录日志并退出程序。
Fatal(ctx context.Context, msg string, keys ...KeyValue)
}
LoggerFatal 支持致命日志和 panic。
提供 Panic、Fatal、DPanic 方法。
type LoggerWithCaller ¶
LoggerWithCaller 支持调用者信息。
type LoggerWithLevel ¶
type LoggerWithLevel interface {
Logger
Log(ctx context.Context, level Level, msg string, keys ...KeyValue)
}
LoggerWithLevel 支持自定义日志级别。
type LoggerWithName ¶
LoggerWithName 支持日志命名。
type LoggerWithTimeout ¶
LoggerWithTimeout 支持超时日志。
type Option ¶
type Option func(*SlogLogger)
Option 定义 slog 日志配置选项
func WithDevelopment ¶ added in v0.0.3
WithDevelopment 设置开发模式
type RandomSampler ¶
type RandomSampler struct {
// contains filtered or unexported fields
}
RandomSampler 随机采样器。
func (*RandomSampler) ShouldSample ¶
func (s *RandomSampler) ShouldSample() bool
ShouldSample 判断是否采样
type SampledLogger ¶
type SampledLogger struct {
// contains filtered or unexported fields
}
SampledLogger 带采样的日志器。
func NewSampledLogger ¶
func NewSampledLogger(logger Logger, sampler Sampler) *SampledLogger
NewSampledLogger 创建带采样的日志器
func (*SampledLogger) DPanic ¶
func (l *SampledLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
DPanic 记录致命错误日志并 panic
func (*SampledLogger) Debug ¶
func (l *SampledLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
Debug 记录调试日志
func (*SampledLogger) Error ¶
func (l *SampledLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
Error 记录错误日志(错误日志不采样,全部记录)
func (*SampledLogger) Fatal ¶
func (l *SampledLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
Fatal 记录致命级别日志
func (*SampledLogger) Info ¶
func (l *SampledLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
Info 记录信息日志
func (*SampledLogger) Panic ¶
func (l *SampledLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
Panic 记录日志并 panic
type SlogLogger ¶
type SlogLogger struct {
// contains filtered or unexported fields
}
SlogLogger 是 slog 日志适配器,实现 Logger 接口
func (*SlogLogger) DPanic ¶
func (l *SlogLogger) DPanic(ctx context.Context, msg string, keys ...KeyValue)
DPanic 记录致命错误日志
在开发模式下会 panic,在生产模式下仅记录 Error 级别日志。 这符合 DPanic 的标准语义:用于检测不应发生的编程错误。
func (*SlogLogger) Debug ¶
func (l *SlogLogger) Debug(ctx context.Context, msg string, keys ...KeyValue)
Debug 记录调试日志
func (*SlogLogger) Error ¶
func (l *SlogLogger) Error(ctx context.Context, msg string, keys ...KeyValue)
Error 记录错误日志
func (*SlogLogger) Fatal ¶
func (l *SlogLogger) Fatal(ctx context.Context, msg string, keys ...KeyValue)
Fatal 记录致命级别日志
注意:与标准 log.Fatal 不同,此方法仅记录日志,不会调用 os.Exit(1)。 如需退出程序,调用方需自行处理。
func (*SlogLogger) Info ¶
func (l *SlogLogger) Info(ctx context.Context, msg string, keys ...KeyValue)
Info 记录信息日志
func (*SlogLogger) Panic ¶
func (l *SlogLogger) Panic(ctx context.Context, msg string, keys ...KeyValue)
Panic 记录日志并 panic
type ThresholdSampler ¶
type ThresholdSampler struct {
// contains filtered or unexported fields
}
ThresholdSampler 阈值采样器。
func NewThresholdSampler ¶
func NewThresholdSampler(threshold int64) *ThresholdSampler
NewThresholdSampler 创建阈值采样器
func (*ThresholdSampler) ShouldSample ¶
func (s *ThresholdSampler) ShouldSample() bool
ShouldSample 判断是否采样