Documentation
¶
Index ¶
- Variables
- func NewJSONLogger(debug bool, w io.Writer, sampler Sampler) *zap.Logger
- func SetCrashOutput(f *os.File) error
- func SetLevel(l string)
- func SetupSlog(cfg Config) (*slog.Logger, func())
- func WithAttrs(parent context.Context, attrs ...slog.Attr) context.Context
- type Config
- func (c Config) SetDebug(debug bool) Config
- func (c Config) SetDir(dir string) Config
- func (c Config) SetEvents(events Events) Config
- func (c Config) SetLevel(level string) Config
- func (c Config) SetMaxAge(days int) Config
- func (c Config) SetRotationTime(rotationTime time.Duration) Config
- func (c Config) SetSampler(sampler Sampler) Config
- func (c Config) SetService(id, name, version string) Config
- type EventFn
- type Events
- type FileConfig
- type Option
- type Sampler
- type Slog
Constants ¶
This section is empty.
Variables ¶
View Source
var Level = zap.NewAtomicLevelAt(zap.InfoLevel)
Level 日志级别
Functions ¶
func NewJSONLogger ¶
NewJSONLogger 创建JSON日志
Types ¶
type Config ¶
type Config struct {
ServiceID string // 服务 ID(可选)
ServiceName string // 服务名称(可选)
ServiceVersion string // 服务版本(可选)
Debug bool // 是否开启 debug,日志会同时写终端和文件
Level string // debug/info/warn/error
Sampler Sampler // 采样器,用于控制日志写入频率(可选)
Events Events // 日志事件回调,按级别触发(可选),回调同步执行,耗时操作需自行异步化
FileConfig // 日志文件配置
}
func (Config) SetRotationTime ¶ added in v1.7.0
func (Config) SetSampler ¶ added in v1.4.1
SetSampler 设置采样器,用于控制日志写入频率(可选)
func (Config) SetService ¶ added in v1.4.1
SetService 设置服务信息,可选 - 使用 id 作为服务唯一标识 - 使用 name 作为服务名称 - 使用 version 作为服务版本 id,name,version 空串时,将不记录到日志
type EventFn ¶ added in v1.7.9
EventFn 日志事件回调,在对应级别的日志写入前同步触发;回调内做耗时操作(如发告警)需自行异步化,否则会拖慢日志写入 回调若需异步留存 record,必须先调用 record.Clone(),否则与后续日志共享底层数据存在竞态
type FileConfig ¶ added in v1.7.0
type Slog ¶ added in v1.2.12
type Slog struct {
// contains filtered or unexported fields
}
Slog 包装任意 slog.Handler,在写日志前按级别分发事件回调 支持 WithAttrs 批量把字段挂到上下文,后续用该上下文打日志时自动带出这些字段
func (*Slog) Handle ¶ added in v1.2.12
Handle 先按级别分发事件回调,再交给底层 handler 写日志;事件在底层 handler 之前触发,被采样丢弃的日志同样会触发回调
Click to show internal directories.
Click to hide internal directories.