Documentation
¶
Index ¶
- func Close() error
- func ContextWithRequestID(ctx context.Context, id string) context.Context
- func ContextWithTraceID(ctx context.Context, id string) context.Context
- func Debug(msg string, fields ...Field)
- func Error(msg string, fields ...Field)
- func Fatal(msg string, fields ...Field)
- func FileCallerEncoder(file string, line int) string
- func FullCallerEncoder(file string, line int) string
- func Info(msg string, fields ...Field)
- func Init(cfg Config, opts ...Option) error
- func RequestIDFromContext(ctx context.Context) (string, bool)
- func SetLevel(lv Level)
- func Sync() error
- func TraceIDFromContext(ctx context.Context) (string, bool)
- func Warn(msg string, fields ...Field)
- type CallerEncoder
- type Config
- type Field
- func Any(k string, v any) Field
- func Bool(k string, v bool) Field
- func Duration(k string, v time.Duration) Field
- func Err(err error) Field
- func Float64(k string, v float64) Field
- func Int(k string, v int) Field
- func Int64(k string, v int64) Field
- func String(k, v string) Field
- func Time(k string, v time.Time) Field
- type FileConfig
- type Interval
- type Level
- type Logger
- type Option
- func WithBuffer(sizeKB int, flushInterval time.Duration) Option
- func WithCaller(on bool) Option
- func WithCallerEncoder(enc CallerEncoder) Option
- func WithConsole(on bool) Option
- func WithConsoleSeparator(sep string) Option
- func WithEncoding(e string) Option
- func WithFile(f FileConfig) Option
- func WithLevel(l Level) Option
- func WithSplitLevel(on bool) Option
- func WithTimeFormat(f string) Option
- func WithWriteErrorHook(hook func(error)) Option
- type Rotator
- type RotatorConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithRequestID ¶
ContextWithRequestID 向 ctx 注入 request_id。
func ContextWithTraceID ¶
ContextWithTraceID 向 ctx 注入 trace_id。
func FileCallerEncoder ¶ added in v0.3.0
FileCallerEncoder 仅输出文件名:行号,如 "global.go:72"。
func FullCallerEncoder ¶ added in v0.3.0
FullCallerEncoder 输出完整路径:行号,如 "/home/user/app/main.go:42"。
func RequestIDFromContext ¶
RequestIDFromContext 提取 request_id。
func TraceIDFromContext ¶
TraceIDFromContext 提取 trace_id。
Types ¶
type CallerEncoder ¶ added in v0.3.0
CallerEncoder 定义 caller 字段格式化方式。传入文件完整路径和行号,返回格式化字符串。
type Config ¶
type Config struct {
Level Level // 最低输出级别,默认 INFO
Encoding string // "json" | "console",默认 json
Console bool // 是否同时输出控制台,默认 false
File FileConfig // 文件输出,Path 为空则不写文件
SplitLevel bool // 是否拆 .log/.log.wf,默认 true
Caller bool // 是否打印 file:line,默认 true
CallerEncoder CallerEncoder // nil 时默认:package/file.go:line(zap ShortCallerEncoder)
TimeFormat string // 时间格式,默认 RFC3339
// ConsoleSeparator 仅在 Encoding="console" 时生效,为空则沿用 zap 默认的制表符。
ConsoleSeparator string
// WriteErrorHook 在底层日志文件写入失败时被回调(如磁盘写满、IO 错误),
// 便于应用埋点/告警/降级。为 nil 时不启用(zap 仍会把写错误输出到 stderr)。
// 回调会在写入路径上同步触发、可能被多 goroutine 并发调用,因此必须并发安全,
// 且不要在其中再写本 logger,以免递归。
WriteErrorHook func(error)
}
Config 日志器配置。
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field 结构化日志字段。内部持有 zap 字段,对外完全隐藏 zap。
type FileConfig ¶
type FileConfig struct {
Path string // 如 "logs/app.log";wf 文件自动派生 "logs/app.log.wf"
MaxSize int // MB,默认 100;0 不按大小轮转
MaxBackups int // 保留个数,默认 10;0 不限
MaxAge int // 保留天数,默认 30;0 不限
Compress bool // gzip 旧文件,默认 false
Interval Interval // 时间轮转粒度,默认 None
// BufferSize 写缓冲大小(KB)。为 0(默认)表示不缓冲,每条日志立即写入底层文件;
// 大于 0 时启用带缓冲的刷盘:日志先写入内存缓冲,缓冲写满或到达 FlushInterval 时才落盘,
// 可显著降低高并发下的 write 系统调用次数。
// 权衡:开启缓冲后会有一个后台定时 flush goroutine(Close 时停止);进程异常退出
// (panic 未 recover、被 kill -9 等)可能丢失尚未 flush 的缓冲日志。Fatal、Sync、Close
// 都会主动 flush,因此正常退出路径不丢日志。此外,写失败时 WriteErrorHook 会在 flush
// 时刻(而非日志调用时刻)被回调。
BufferSize int
// FlushInterval 缓冲定时 flush 间隔,仅在 BufferSize>0 时生效。
// 为 0 时启用缓冲则采用 zap 默认值(30s)。
FlushInterval time.Duration
}
FileConfig 文件输出与轮转配置。
type Logger ¶
type Logger interface {
Debug(msg string, fields ...Field)
Info(msg string, fields ...Field)
Warn(msg string, fields ...Field)
Error(msg string, fields ...Field)
Fatal(msg string, fields ...Field) // 写完 Sync 后 os.Exit(1)
With(fields ...Field) Logger
WithContext(ctx context.Context) Logger
// WithCaller 返回派生 Logger,动态开启/关闭自动 caller 注解。
// enabled=false 时不再自动输出 caller 字段,便于调用方按自身逻辑
// (如动态回溯栈帧)手动写入一个名为 "caller" 的字段。
// 派生实例复用同一底层 core/sink,Close 仍只应在根 logger 上调用。
WithCaller(enabled bool) Logger
SetLevel(l Level)
Sync() error
Close() error
}
Logger 日志器接口。日志方法绝不 panic、绝不返回 error。
func Default ¶ added in v0.3.3
func Default() Logger
Default 返回当前全局 Logger 实例;未 Init 时惰性装配默认 stderr logger。
func WithContext ¶
type Option ¶
type Option func(*Config)
Option 以 functional options 方式修改 Config。
func WithBuffer ¶ added in v0.2.0
WithBuffer 启用文件写缓冲:sizeKB 为缓冲大小(KB),<=0 表示关闭缓冲(默认)。 flushInterval 为定时 flush 间隔,为 0 时启用缓冲则采用 zap 默认值(30s)。 开启后会有一个后台定时 flush goroutine,Close 时停止;详见 FileConfig.BufferSize。
func WithCaller ¶
func WithCallerEncoder ¶ added in v0.3.0
func WithCallerEncoder(enc CallerEncoder) Option
func WithConsole ¶
func WithConsoleSeparator ¶
WithConsoleSeparator 定制 console 编码的字段分隔符(仅 Encoding="console" 生效), 为空则沿用 zap 默认的制表符。
func WithEncoding ¶
func WithFile ¶
func WithFile(f FileConfig) Option
func WithSplitLevel ¶
func WithTimeFormat ¶
func WithWriteErrorHook ¶ added in v0.2.0
WithWriteErrorHook 注册日志文件写入失败时的回调(如磁盘写满、IO 错误)。 回调会在写入路径上同步触发、可能被并发调用,必须并发安全, 且不要在其中再写本 logger,以免递归。
type Rotator ¶
type Rotator struct {
// contains filtered or unexported fields
}
Rotator 自研文件轮转写入器,支持大小+时间叠加轮转。
func NewRotator ¶
func NewRotator(cfg RotatorConfig) *Rotator
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Example: basic
|
Example: basic |
|
buffered
command
Example: buffered
|
Example: buffered |
|
context
command
Example: context
|
Example: context |
|
file-rotation
command
Example: file-rotation
|
Example: file-rotation |
|
gin
command
Example: gin
|
Example: gin |
|
instance
command
Example: instance
|
Example: instance |
|
middleware
|
|