Documentation
¶
Overview ¶
Package slogx: Lightweight package around slog with caller skip support Provides zap.AddCallerSkip-like function that standard log/slog package lacks Solves caller location issues in logging assist functions with 1:1 API compatible support
slogx: 提供支持 caller skip 的 slog 轻量包装器 为标准 log/slog 包提供类似 zap.AddCallerSkip 的功能 在保持 1:1 API 兼容的前提下解决日志助手函数中的调用位置问题
Index ¶
- Variables
- func SetLog(slog *slog.Logger)
- type Logger
- func (G *Logger) Debug(msg string, args ...any)
- func (G *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (G *Logger) Enabled(ctx context.Context, level slog.Level) bool
- func (G *Logger) Error(msg string, args ...any)
- func (G *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (G *Logger) Handler() slog.Handler
- func (G *Logger) Info(msg string, args ...any)
- func (G *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (G *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func (G *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func (G *Logger) Skip(depth int) *Logger
- func (G *Logger) Sugar() *SugaredLogger
- func (G *Logger) Warn(msg string, args ...any)
- func (G *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (G *Logger) With(args ...any) *Logger
- func (G *Logger) WithCallerSkip(depth int) *Logger
- func (G *Logger) WithGroup(name string) *Logger
- type SugaredLogger
- func (s *SugaredLogger) Debug(args ...any)
- func (s *SugaredLogger) DebugContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Debugln(args ...any)
- func (s *SugaredLogger) DebuglnContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Error(args ...any)
- func (s *SugaredLogger) ErrorContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Errorln(args ...any)
- func (s *SugaredLogger) ErrorlnContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Info(args ...any)
- func (s *SugaredLogger) InfoContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Infoln(args ...any)
- func (s *SugaredLogger) InfolnContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Warn(args ...any)
- func (s *SugaredLogger) WarnContext(ctx context.Context, args ...any)
- func (s *SugaredLogger) Warnln(args ...any)
- func (s *SugaredLogger) WarnlnContext(ctx context.Context, args ...any)
Constants ¶
This section is empty.
Variables ¶
var LOG = NewDefault()
LOG provides structured attribute logging with pairs LOG 提供键值对日志记录
var SUG = NewSugaredLogger(LOG)
SUG provides flattened argument logging like zap.SugaredLogger SUG 提供平铺参数日志记录,类似 zap.SugaredLogger
Functions ¶
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a lightweight package that supports AddCallerSkip-like action Provides 1:1 API compatible support with slog.Logger while enabling correct caller skip Uses runtime.Callers to adjust code position to ensure accurate source location reporting
Logger 是一个轻量包装器,支持 AddCallerSkip 类似的行为 与 slog.Logger 提供 1:1 API 兼容性,同时支持正确的调用者跳过 使用 runtime.Callers 调整程序计数器,确保准确的源位置报告
func New ¶
New creates a package with given logger. Logger must not be none Sets default skip value to 4 to ensure accurate caller location reporting Returns reference to enable method chaining and correct cloning action
New 用给定 logger 创建包装器。Logger 不能为 none 设置默认 skip 值为 4,确保准确的调用者位置报告 返回指针以支持方法链式调用和正确的克隆行为
func NewDefault ¶
func NewDefault() *Logger
NewDefault creates a Logger with default JSON handler configuration Auto configures with source location and debug level output
NewDefault 创建具有默认 JSON 处理器配置的 Logger 自动配置源位置和调试级别输出
func (*Logger) DebugContext ¶
DebugContext logs a debug message with context and key-value pairs DebugContext 记录带上下文和键值对的调试消息
func (*Logger) Enabled ¶
Enabled checks if the log instance emits log records at the given log-level Enabled 检查日志实例是否在给定日志级别发出日志记录
func (*Logger) ErrorContext ¶
ErrorContext logs an error message with context and key-value pairs ErrorContext 记录带上下文和键值对的错误消息
func (*Logger) Handler ¶
Handler returns the base slog.Handler component Handler 返回底层 slog.Handler 组件
func (*Logger) InfoContext ¶
InfoContext logs an info message with context and key-value pairs InfoContext 记录带上下文和键值对的信息消息
func (*Logger) Log ¶
Log logs a message with key-value pairs at given level (generic logging method) Log 在指定级别记录带键值对的消息(通用日志方法)
func (*Logger) LogAttrs ¶
LogAttrs logs a message with structured attributes at given level LogAttrs 在指定级别记录带结构化属性的消息
func (*Logger) Skip ¶
Skip is a convenience alias of WithCallerSkip method Provides short method name in common caller skip operations
Skip 是 WithCallerSkip 方法的便利别名 为常用调用者跳过操作提供更短的方法名
func (*Logger) Sugar ¶
func (G *Logger) Sugar() *SugaredLogger
Sugar returns a SugaredLogger to enable flattened argument logging Sugar 返回用于平铺参数日志记录的 SugaredLogger
func (*Logger) WarnContext ¶
WarnContext logs a warn message with context and key-value pairs WarnContext 记录带上下文和键值对的警告消息
func (*Logger) With ¶
With returns a new Logger that includes the given arguments Creates a clone with extra context fields attached to subsequent log entries
With 返回包含给定参数的新 Logger 创建一个带有额外上下文字段的克隆,附加到后续日志条目
func (*Logger) WithCallerSkip ¶
WithCallerSkip returns a new Logger with extra stack frame skip Creates a clone with adjusted skip value in nested assist functions Enables accurate source location reporting in multi-stage package scenarios
WithCallerSkip 返回一个新的 Logger,增加跳过的栈帧层数 为嵌套助手函数创建带有调整 skip 值的克隆 在多层包装器场景中实现准确的源位置报告
type SugaredLogger ¶
type SugaredLogger struct {
// contains filtered or unexported fields
}
SugaredLogger provides zap.SugaredLogger-like interface with flattened arguments Converts arguments into a single message string using fmt.Sprint
SugaredLogger 提供类似 zap.SugaredLogger 的平铺参数接口 使用 fmt.Sprint 将参数转换为单个消息字符串
func NewSugaredLogger ¶
func NewSugaredLogger(logger *Logger) *SugaredLogger
NewSugaredLogger creates a new SugaredLogger with flattened argument support NewSugaredLogger 创建支持平铺参数的新 SugaredLogger
func (*SugaredLogger) Debug ¶
func (s *SugaredLogger) Debug(args ...any)
Debug logs flattened arguments at debug level (like fmt.Sprint) Debug 在调试级别记录平铺参数(类似 fmt.Sprint)
func (*SugaredLogger) DebugContext ¶
func (s *SugaredLogger) DebugContext(ctx context.Context, args ...any)
DebugContext logs flattened arguments at debug level with context DebugContext 在调试级别记录平铺参数,带上下文
func (*SugaredLogger) Debugln ¶
func (s *SugaredLogger) Debugln(args ...any)
Debugln logs flattened arguments at debug level with spaces (like fmt.Sprintln) Debugln 在调试级别记录平铺参数,用空格分隔(类似 fmt.Sprintln)
func (*SugaredLogger) DebuglnContext ¶
func (s *SugaredLogger) DebuglnContext(ctx context.Context, args ...any)
DebuglnContext logs flattened arguments at debug level with context and spaces DebuglnContext 在调试级别记录平铺参数,带上下文,用空格分隔
func (*SugaredLogger) Error ¶
func (s *SugaredLogger) Error(args ...any)
Error logs flattened arguments at error level (like fmt.Sprint) Error 在错误级别记录平铺参数(类似 fmt.Sprint)
func (*SugaredLogger) ErrorContext ¶
func (s *SugaredLogger) ErrorContext(ctx context.Context, args ...any)
ErrorContext logs flattened arguments at error level with context ErrorContext 在错误级别记录平铺参数,带上下文
func (*SugaredLogger) Errorln ¶
func (s *SugaredLogger) Errorln(args ...any)
Errorln logs flattened arguments at error level with spaces (like fmt.Sprintln) Errorln 在错误级别记录平铺参数,用空格分隔(类似 fmt.Sprintln)
func (*SugaredLogger) ErrorlnContext ¶
func (s *SugaredLogger) ErrorlnContext(ctx context.Context, args ...any)
ErrorlnContext logs flattened arguments at error level with context and spaces ErrorlnContext 在错误级别记录平铺参数,带上下文,用空格分隔
func (*SugaredLogger) Info ¶
func (s *SugaredLogger) Info(args ...any)
Info logs flattened arguments at info level (like fmt.Sprint) Info 在信息级别记录平铺参数(类似 fmt.Sprint)
func (*SugaredLogger) InfoContext ¶
func (s *SugaredLogger) InfoContext(ctx context.Context, args ...any)
InfoContext logs flattened arguments at info level with context InfoContext 在信息级别记录平铺参数,带上下文
func (*SugaredLogger) Infoln ¶
func (s *SugaredLogger) Infoln(args ...any)
Infoln logs flattened arguments at info level with spaces (like fmt.Sprintln) Infoln 在信息级别记录平铺参数,用空格分隔(类似 fmt.Sprintln)
func (*SugaredLogger) InfolnContext ¶
func (s *SugaredLogger) InfolnContext(ctx context.Context, args ...any)
InfolnContext logs flattened arguments at info level with context and spaces InfolnContext 在信息级别记录平铺参数,带上下文,用空格分隔
func (*SugaredLogger) Warn ¶
func (s *SugaredLogger) Warn(args ...any)
Warn logs flattened arguments at warn level (like fmt.Sprint) Warn 在警告级别记录平铺参数(类似 fmt.Sprint)
func (*SugaredLogger) WarnContext ¶
func (s *SugaredLogger) WarnContext(ctx context.Context, args ...any)
WarnContext logs flattened arguments at warn level with context WarnContext 在警告级别记录平铺参数,带上下文
func (*SugaredLogger) Warnln ¶
func (s *SugaredLogger) Warnln(args ...any)
Warnln logs flattened arguments at warn level with spaces (like fmt.Sprintln) Warnln 在警告级别记录平铺参数,用空格分隔(类似 fmt.Sprintln)
func (*SugaredLogger) WarnlnContext ¶
func (s *SugaredLogger) WarnlnContext(ctx context.Context, args ...any)
WarnlnContext logs flattened arguments at warn level with context and spaces WarnlnContext 在警告级别记录平铺参数,带上下文,用空格分隔