logging

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Default 是一个可选的全局 Logger, 方便快速集成。

Functions

func Debug

func Debug(ctx context.Context, msg string, fields map[string]any)

Helper 函数, 便于直接调用 logging.Info(ctx, ...)

func Error

func Error(ctx context.Context, msg string, fields map[string]any)

func Flush

func Flush(ctx context.Context)

func Info

func Info(ctx context.Context, msg string, fields map[string]any)

func Warn

func Warn(ctx context.Context, msg string, fields map[string]any)

Types

type ComponentLogger added in v0.25.0

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

ComponentLogger 带组件名称的 Logger,便于追踪日志来源

func ForComponent added in v0.25.0

func ForComponent(component string) *ComponentLogger

ForComponent 创建带组件名称的 Logger

func (*ComponentLogger) Debug added in v0.25.0

func (c *ComponentLogger) Debug(ctx context.Context, msg string, fields map[string]any)

func (*ComponentLogger) Error added in v0.25.0

func (c *ComponentLogger) Error(ctx context.Context, msg string, fields map[string]any)

func (*ComponentLogger) Info added in v0.25.0

func (c *ComponentLogger) Info(ctx context.Context, msg string, fields map[string]any)

func (*ComponentLogger) Printf added in v0.25.0

func (c *ComponentLogger) Printf(format string, args ...any)

Printf 兼容 log.Printf 的接口,便于迁移 用法: logger.Printf("message %s", arg) 替代 log.Printf("[Component] message %s", arg)

func (*ComponentLogger) Warn added in v0.25.0

func (c *ComponentLogger) Warn(ctx context.Context, msg string, fields map[string]any)

type FileTransport

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

FileTransport 将日志记录以 JSON 行写入到指定文件

func NewFileTransport

func NewFileTransport(path string) (*FileTransport, error)

NewFileTransport 创建 FileTransport, path 为日志文件路径

func (*FileTransport) Close

func (t *FileTransport) Close() error

Close 关闭底层文件

func (*FileTransport) Flush

func (t *FileTransport) Flush(ctx context.Context) error

func (*FileTransport) Log

func (t *FileTransport) Log(ctx context.Context, rec *LogRecord) error

func (*FileTransport) Name

func (t *FileTransport) Name() string

type Level

type Level string

Level 日志级别

const (
	LevelDebug Level = "debug"
	LevelInfo  Level = "info"
	LevelWarn  Level = "warn"
	LevelError Level = "error"
)

type LogRecord

type LogRecord struct {
	Timestamp time.Time      `json:"ts"`
	Level     Level          `json:"level"`
	Message   string         `json:"message"`
	Fields    map[string]any `json:"fields,omitempty"`
}

LogRecord 标准化日志记录结构

type Logger

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

Logger 聚合多个 Transport, 提供统一的日志接口

func NewLogger

func NewLogger(level Level, transports ...Transport) *Logger

NewLogger 创建 Logger 实例

func (*Logger) AddTransport

func (l *Logger) AddTransport(t Transport)

AddTransport 动态添加 transport

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, fields map[string]any)

Debug 记录调试日志

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, fields map[string]any)

Error 记录错误日志

func (*Logger) Flush

func (l *Logger) Flush(ctx context.Context)

Flush 刷新所有 transports

func (*Logger) ForComponent added in v0.25.0

func (l *Logger) ForComponent(component string) *ComponentLogger

WithLogger 使用指定的 Logger 创建 ComponentLogger

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, fields map[string]any)

Info 记录信息日志

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel 设置日志级别

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, fields map[string]any)

Warn 记录警告日志

type StdoutTransport

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

StdoutTransport 将日志记录以 JSON 行的形式写到 stdout

func NewStdoutTransport

func NewStdoutTransport() *StdoutTransport

NewStdoutTransport 创建 StdoutTransport

func (*StdoutTransport) Flush

func (t *StdoutTransport) Flush(ctx context.Context) error

func (*StdoutTransport) Log

func (t *StdoutTransport) Log(ctx context.Context, rec *LogRecord) error

func (*StdoutTransport) Name

func (t *StdoutTransport) Name() string

type Transport

type Transport interface {
	// Name 返回 transport 名称(用于调试)
	Name() string
	// Log 写入一条日志记录
	Log(ctx context.Context, rec *LogRecord) error
	// Flush 刷新缓冲(如果有)
	Flush(ctx context.Context) error
}

Transport 日志输出通道接口 设计参考: 常见的多目标日志管道实现, 但尽量保持简单

Jump to

Keyboard shortcuts

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