Documentation
¶
Index ¶
- Variables
- func Debug(ctx context.Context, msg string, fields map[string]any)
- func Error(ctx context.Context, msg string, fields map[string]any)
- func Flush(ctx context.Context)
- func Info(ctx context.Context, msg string, fields map[string]any)
- func Warn(ctx context.Context, msg string, fields map[string]any)
- type ComponentLogger
- func (c *ComponentLogger) Debug(ctx context.Context, msg string, fields map[string]any)
- func (c *ComponentLogger) Error(ctx context.Context, msg string, fields map[string]any)
- func (c *ComponentLogger) Info(ctx context.Context, msg string, fields map[string]any)
- func (c *ComponentLogger) Printf(format string, args ...any)
- func (c *ComponentLogger) Warn(ctx context.Context, msg string, fields map[string]any)
- type FileTransport
- type Level
- type LogRecord
- type Logger
- func (l *Logger) AddTransport(t Transport)
- func (l *Logger) Debug(ctx context.Context, msg string, fields map[string]any)
- func (l *Logger) Error(ctx context.Context, msg string, fields map[string]any)
- func (l *Logger) Flush(ctx context.Context)
- func (l *Logger) ForComponent(component string) *ComponentLogger
- func (l *Logger) Info(ctx context.Context, msg string, fields map[string]any)
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) Warn(ctx context.Context, msg string, fields map[string]any)
- type StdoutTransport
- type Transport
Constants ¶
This section is empty.
Variables ¶
View Source
var Default = NewLogger(LevelInfo, NewStdoutTransport())
Default 是一个可选的全局 Logger, 方便快速集成。
Functions ¶
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) 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)
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) Name ¶
func (t *FileTransport) Name() string
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 (*Logger) AddTransport ¶
AddTransport 动态添加 transport
func (*Logger) ForComponent ¶ added in v0.25.0
func (l *Logger) ForComponent(component string) *ComponentLogger
WithLogger 使用指定的 Logger 创建 ComponentLogger
type StdoutTransport ¶
type StdoutTransport struct {
// contains filtered or unexported fields
}
StdoutTransport 将日志记录以 JSON 行的形式写到 stdout
func NewStdoutTransport ¶
func NewStdoutTransport() *StdoutTransport
NewStdoutTransport 创建 StdoutTransport
func (*StdoutTransport) Log ¶
func (t *StdoutTransport) Log(ctx context.Context, rec *LogRecord) error
func (*StdoutTransport) Name ¶
func (t *StdoutTransport) Name() string
Click to show internal directories.
Click to hide internal directories.