log

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 7 Imported by: 0

README

log 包 — 日志抽象层

所属层级: Infrastructure Layer
设计理念: 统一接口,多实现支持
设计灵感: SLF4J + Go slog

概述

log 包提供统一的日志记录接口抽象,允许不同的日志库实现无缝替换。内置基于 Go 标准库 log/slog 的默认实现 SlogLogger,支持 JSON 和 text 两种输出格式。

核心功能
功能 说明
统一接口 Logger 接口定义标准日志记录操作
多级别支持 Debug、Info、Warn、Error、DPanic、Panic、Fatal
结构化日志 KeyValue 支持结构化数据记录
多格式输出 JSON 和 text 两种输出格式
扩展接口 支持级别、名称、调用者、超时等扩展功能
依赖注入 可与 IoC 容器集成

核心接口

Logger 接口
type Logger interface {
    Debug(ctx context.Context, msg string, keys ...KeyValue)
    Info(ctx context.Context, msg string, keys ...KeyValue)
    Warn(ctx context.Context, msg string, keys ...KeyValue)
    Error(ctx context.Context, msg string, keys ...KeyValue)
    DPanic(ctx context.Context, msg string, keys ...KeyValue)
    Panic(ctx context.Context, msg string, keys ...KeyValue)
    Fatal(ctx context.Context, msg string, keys ...KeyValue)
    Sync() error
    With(ctx context.Context, keys ...KeyValue) Logger
}
方法说明
方法 说明
Debug 调试日志
Info 信息日志
Warn 警告日志
Error 错误日志
DPanic 致命错误日志,开发环境触发 panic
Panic 记录日志并 panic
Fatal 记录日志(注意:不主动调用 os.Exit(1)
Sync 同步日志缓冲区
With 返回带有额外固定字段的新 Logger
Level — 日志级别
type Level int8

const (
    DebugLevel  Level = iota // 调试级别
    InfoLevel                // 信息级别
    WarnLevel                // 警告级别
    ErrorLevel               // 错误级别
    DPanicLevel              // 致命错误级别(开发环境 panic)
    PanicLevel               // panic 级别
    FatalLevel               // 致命级别(程序退出)
)

字符串表示:"debug""info""warn""error""dpanic""panic""fatal"

KeyValue — 结构化键值对
type KeyValue struct {
    Key   string
    Value any
}

用于结构化日志记录:

log.KeyValue{Key: "user_id", Value: 123}
log.KeyValue{Key: "duration", Value: time.Second}
扩展接口
接口 说明
LoggerWithLevel 支持在运行时使用任意级别记录日志
LoggerWithName 支持为 Logger 设置名称(如模块名)
LoggerWithCaller 支持记录调用者源码位置信息
LoggerWithTimeout 支持超时自动刷盘
type LoggerWithLevel interface {
    Logger
    Log(ctx context.Context, level Level, msg string, keys ...KeyValue)
}

type LoggerWithName interface {
    Logger
    WithName(name string) Logger
}

type LoggerWithCaller interface {
    Logger
    WithCaller(skip int) Logger
}

type LoggerWithTimeout interface {
    Logger
    WithTimeout(d time.Duration) Logger
}

快速开始

创建日志记录器
package main

import (
    "context"
    "github.com/xudefa/enhance/log"
)

func main() {
    // 默认配置(JSON 格式,Info 级别,标准输出)
    logger := log.NewSlogLogger()
    
    ctx := context.Background()
    
    logger.Info(ctx, "服务启动", log.KeyValue{Key: "port", Value: 8080})
    logger.Debug(ctx, "SQL 查询", log.KeyValue{Key: "sql", Value: "SELECT * FROM users"})
    logger.Error(ctx, "请求失败",
        log.KeyValue{Key: "path", Value: "/api/users"},
        log.KeyValue{Key: "status", Value: 500},
    )
}
自定义配置
logger := log.NewSlogLogger(
    log.WithLevel(log.DebugLevel),
    log.WithFormat("text"),
    log.WithTimeFormat("2006-01-02 15:04:05"),
    log.WithAddSource(true),
    log.WithOutput(os.Stderr),
    log.WithOutputPath("/var/log/app.log"),
)

API 参考

SlogLogger — 基于 slog 的实现
创建
// 默认配置
logger := log.NewSlogLogger()

// 自定义配置
logger := log.NewSlogLogger(
    log.WithLevel(log.DebugLevel),
    log.WithFormat("text"),
    log.WithTimeFormat("2006-01-02 15:04:05"),
    log.WithAddSource(true),
    log.WithOutput(os.Stderr),
    log.WithOutputPath("/var/log/app.log"),
)
Option
选项 说明
WithLevel(level Level) 设置日志级别(默认 Info)
WithFormat(format string) 输出格式:"json"(默认)或 "text"
WithTimeFormat(timeFormat string) 时间格式(默认 "2006-01-02 15:04:05"
WithAddSource(addSource bool) 是否添加源码位置(默认 false)
WithOutput(w io.Writer) 设置输出 Writer(默认 os.Stdout)
WithOutputPath(path string) 设置日志文件输出路径
Close

关闭日志文件句柄(仅在设置了文件输出时需要调用):

if closer, ok := logger.(*log.SlogLogger); ok {
    defer closer.Close()
}
Build — 日志记录器构建
func Build(opts ...LoggerOption) Logger

type LoggerOption func(*loggerConfig)

func WithLogger(logger Logger) LoggerOption

使用示例:

logger := log.Build(log.WithLogger(log.NewSlogLogger(
    log.WithFormat("text"),
)))
ToLevel — 级别转换

将字符串转换为日志级别:

level := log.ToLevel("info") // log.InfoLevel
level := log.ToLevel("warn") // log.WarnLevel

使用示例

基础日志记录
logger := log.NewSlogLogger(
    log.WithLevel(log.DebugLevel),
    log.WithFormat("json"),
)

ctx := context.Background()

logger.Info(ctx, "服务启动", log.KeyValue{Key: "port", Value: 8080})
logger.Debug(ctx, "SQL 查询", log.KeyValue{Key: "sql", Value: "SELECT * FROM users"})
logger.Error(ctx, "请求失败",
    log.KeyValue{Key: "path", Value: "/api/users"},
    log.KeyValue{Key: "status", Value: 500},
)
使用 With 添加固定字段
// 使用 With 添加固定字段
logger2 := logger.With(ctx, log.KeyValue{Key: "service", Value: "user-svc"})
logger2.Info(ctx, "用户注册") // 自动携带 service=user-svc
与依赖注入集成
container.Register(
    reflect.TypeOf(&log.SlogLogger{}),
    core.Bean(log.NewSlogLogger(
        log.WithLevel(log.InfoLevel),
        log.WithFormat("json"),
    )),
    core.Singleton(),
)

type UserService struct {
    Logger log.Logger `inject:"logger"`
}

最佳实践

1. 使用结构化日志
// ✅ 推荐:使用 KeyValue 结构化记录
logger.Info(ctx, "用户登录",
    log.KeyValue{Key: "user_id", Value: 123},
    log.KeyValue{Key: "ip", Value: "192.168.1.1"},
)

// ⚠️ 不推荐:使用字符串拼接
logger.Info(ctx, fmt.Sprintf("用户登录 user_id=%d ip=%s", 123, "192.168.1.1"))
2. 合理设置日志级别
// ✅ 推荐:生产环境使用 Info 级别
logger := log.NewSlogLogger(log.WithLevel(log.InfoLevel))

// ✅ 推荐:开发环境使用 Debug 级别
logger := log.NewSlogLogger(log.WithLevel(log.DebugLevel))

// ⚠️ 不推荐:生产环境使用 Debug 级别,影响性能
logger := log.NewSlogLogger(log.WithLevel(log.DebugLevel))
3. 使用 With 添加上下文信息
// ✅ 推荐:为每个请求添加 trace_id
requestLogger := logger.With(ctx,
    log.KeyValue{Key: "trace_id", Value: traceID},
    log.KeyValue{Key: "user_id", Value: userID},
)
requestLogger.Info(ctx, "处理请求")

// ⚠️ 不推荐:每次手动添加
logger.Info(ctx, "处理请求",
    log.KeyValue{Key: "trace_id", Value: traceID},
    log.KeyValue{Key: "user_id", Value: userID},
)
4. 输出到文件
// ✅ 推荐:生产环境输出到文件
logger := log.NewSlogLogger(
    log.WithOutputPath("/var/log/app.log"),
    log.WithFormat("json"),
)
defer logger.(*log.SlogLogger).Close()

// ⚠️ 不推荐:忘记关闭文件句柄
logger := log.NewSlogLogger(log.WithOutputPath("/var/log/app.log"))
5. 与依赖注入集成
// ✅ 推荐:将 Logger 注册为 Bean
container.Register(
    reflect.TypeOf(&log.SlogLogger{}),
    core.Bean(log.NewSlogLogger(
        log.WithLevel(log.InfoLevel),
        log.WithFormat("json"),
    )),
    core.Singleton(),
)

// 注入使用
type UserService struct {
    Logger log.Logger `inject:"logger"`
}

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

Constants

This section is empty.

Variables

View Source
var TraceContextKey = contextKey{}

TraceContextKey 追踪 ID 上下文键

Functions

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 从上下文获取 trace_id

参数:

  • ctx: 上下文

返回:

  • string: trace_id,不存在返回空字符串

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID 将 trace_id 注入上下文

参数:

  • ctx: 原始上下文
  • traceID: 追踪 ID

返回:

  • context.Context: 包含 trace_id 的新上下文

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

func (*ContextLogger) Sync

func (l *ContextLogger) Sync() error

Sync 同步日志缓冲区

func (*ContextLogger) Warn

func (l *ContextLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)

Warn 记录警告日志

func (*ContextLogger) With

func (l *ContextLogger) With(ctx context.Context, keys ...KeyValue) Logger

With 返回带有额外字段的日志记录器

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: 新的日志级别

func (*DynamicLevelLogger) Sync

func (d *DynamicLevelLogger) Sync() error

Sync 同步日志缓冲区

func (*DynamicLevelLogger) Warn

func (d *DynamicLevelLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)

Warn 记录警告日志

func (*DynamicLevelLogger) With

func (d *DynamicLevelLogger) With(ctx context.Context, keys ...KeyValue) Logger

With 返回带有额外字段的日志记录器

type KeyValue

type KeyValue struct {
	Key   string // 字段名
	Value any    // 字段值
}

KeyValue 定义日志键值对。

用于结构化日志记录,支持键值对形式的日志字段。

type Level

type Level int8

Level 定义日志级别。

日志级别从低到高,用于控制日志输出的详细程度。 生产环境推荐使用 InfoLevel 或 WarnLevel。

级别说明

  • DebugLevel: 调试级别,详细的调试信息,生产环境通常禁用
  • InfoLevel: 信息级别,一般运行信息,如启动/关闭
  • WarnLevel: 警告级别,潜在问题,但不影响正常运行
  • ErrorLevel: 错误级别,错误信息,需要关注但不影响核心功能
  • DPanicLevel: 致命错误级别,开发环境 panic,生产环境仅记录错误
  • PanicLevel: panic 级别,记录日志后 panic
  • FatalLevel: 致命级别,记录日志后退出程序
const (
	DebugLevel  Level = iota // 调试级别:详细的调试信息,生产环境通常禁用
	InfoLevel                // 信息级别:一般运行信息,如启动/关闭
	WarnLevel                // 警告级别:潜在问题,但不影响正常运行
	ErrorLevel               // 错误级别:错误信息,需要关注但不影响核心功能
	DPanicLevel              // 致命错误级别:开发环境 panic,生产环境仅记录错误
	PanicLevel               // panic 级别:记录日志后 panic
	FatalLevel               // 致命级别:记录日志后退出程序
)

日志级别常量定义。

func ToLevel

func ToLevel(level string) Level

ToLevel 将字符串转换为日志级别。

func (Level) String

func (l Level) String() string

String 返回日志级别的字符串表示。

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 NewLoggerBuilder

func NewLoggerBuilder() *LoggerBuilder

NewLoggerBuilder 创建日志器构建器

func (*LoggerBuilder) AddSource

func (b *LoggerBuilder) AddSource(addSource bool) *LoggerBuilder

AddSource 设置是否添加源码位置

func (*LoggerBuilder) Build

func (b *LoggerBuilder) Build() Logger

Build 构建日志器

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) Name

func (b *LoggerBuilder) Name(name string) *LoggerBuilder

Name 设置日志器名称

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 LoggerOption

type LoggerOption func(*loggerConfig)

LoggerOption 定义日志记录器配置选项。

func WithLogger

func WithLogger(logger Logger) LoggerOption

WithLogger 设置日志记录器。

type LoggerWithCaller

type LoggerWithCaller interface {
	Logger
	WithCaller(skip int) Logger
}

LoggerWithCaller 支持调用者信息。

type LoggerWithLevel

type LoggerWithLevel interface {
	Logger
	Log(ctx context.Context, level Level, msg string, keys ...KeyValue)
}

LoggerWithLevel 支持自定义日志级别。

type LoggerWithName

type LoggerWithName interface {
	Logger
	WithName(name string) Logger
}

LoggerWithName 支持日志命名。

type LoggerWithTimeout

type LoggerWithTimeout interface {
	Logger
	WithTimeout(d time.Duration) Logger
}

LoggerWithTimeout 支持超时日志。

type Option

type Option func(*SlogLogger)

Option 定义 slog 日志配置选项

func WithAddSource

func WithAddSource(addSource bool) Option

WithAddSource 设置是否添加源码位置

func WithDevelopment added in v0.0.3

func WithDevelopment(development bool) Option

WithDevelopment 设置开发模式

func WithFormat

func WithFormat(format string) Option

WithFormat 设置输出格式

func WithLevel

func WithLevel(level Level) Option

WithLevel 设置日志级别

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput 设置输出 writer

func WithOutputPath

func WithOutputPath(path string) Option

WithOutputPath 设置日志文件输出路径

func WithTimeFormat

func WithTimeFormat(timeFormat string) Option

WithTimeFormat 设置时间格式

type RandomSampler

type RandomSampler struct {
	// contains filtered or unexported fields
}

RandomSampler 随机采样器。

func NewRandomSampler

func NewRandomSampler(rate float64) *RandomSampler

NewRandomSampler 创建随机采样器

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

func (*SampledLogger) Sync

func (l *SampledLogger) Sync() error

Sync 同步日志缓冲区

func (*SampledLogger) Warn

func (l *SampledLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)

Warn 记录警告日志

func (*SampledLogger) With

func (l *SampledLogger) With(ctx context.Context, keys ...KeyValue) Logger

With 返回带有额外字段的日志记录器

type Sampler

type Sampler interface {
	ShouldSample() bool
}

Sampler 采样策略接口。

type SlogLogger

type SlogLogger struct {
	// contains filtered or unexported fields
}

SlogLogger 是 slog 日志适配器,实现 Logger 接口

func NewSlogLogger

func NewSlogLogger(opts ...Option) *SlogLogger

NewSlogLogger 创建 slog 日志适配器

func (*SlogLogger) Close

func (l *SlogLogger) Close() error

Close 关闭日志文件句柄

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

func (*SlogLogger) Sync

func (l *SlogLogger) Sync() error

Sync 同步日志缓冲区

func (*SlogLogger) Warn

func (l *SlogLogger) Warn(ctx context.Context, msg string, keys ...KeyValue)

Warn 记录警告日志

func (*SlogLogger) With

func (l *SlogLogger) With(ctx context.Context, keys ...KeyValue) Logger

With 返回带有额外字段的日志记录器

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 判断是否采样

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL