log

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug added in v1.0.2

func Debug(msg string, attrs ...slog.Attr)

全局日志函数

func Debugf added in v1.0.2

func Debugf(format string, args ...interface{})

func Error

func Error(msg string, attrs ...slog.Attr)

func ErrorCompat added in v1.0.2

func ErrorCompat(err error, args ...slog.Attr)

ErrorCompat 兼容旧接口

func Errorf added in v1.0.2

func Errorf(format string, args ...interface{})

func Fatal added in v1.0.2

func Fatal(msg string, attrs ...slog.Attr)

func Fatalf added in v1.0.2

func Fatalf(format string, args ...interface{})

func Info

func Info(msg string, attrs ...slog.Attr)

func InfoCompat added in v1.0.2

func InfoCompat(format string, args ...slog.Attr)

InfoCompat 兼容旧接口

func Infof added in v1.0.2

func Infof(format string, args ...interface{})

func SetGlobalLogger added in v1.0.2

func SetGlobalLogger(logger Logger)

SetGlobalLogger 设置全局日志记录器

func Warn added in v1.0.2

func Warn(msg string, attrs ...slog.Attr)

func Warnf added in v1.0.2

func Warnf(format string, args ...interface{})

Types

type ConsoleSink added in v1.0.2

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

ConsoleSink 控制台输出

func NewConsoleSink added in v1.0.2

func NewConsoleSink(cfg ConsoleSinkConfig) *ConsoleSink

func (*ConsoleSink) Close added in v1.0.2

func (c *ConsoleSink) Close() error

func (*ConsoleSink) Write added in v1.0.2

func (c *ConsoleSink) Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error

type ConsoleSinkConfig added in v1.0.2

type ConsoleSinkConfig struct {
	Format string `json:"format" yaml:"format" toml:"format"` // json, text, color
	Output string `json:"output" yaml:"output" toml:"output"` // stdout, stderr
}

ConsoleSinkConfig 控制台输出配置

type DefaultLogger added in v1.0.2

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

DefaultLogger 默认日志记录器实现

func NewDefaultLogger added in v1.0.2

func NewDefaultLogger(sink Sink, level slog.Level) *DefaultLogger

func (*DefaultLogger) Debug added in v1.0.2

func (l *DefaultLogger) Debug(msg string, attrs ...slog.Attr)

func (*DefaultLogger) Debugf added in v1.0.2

func (l *DefaultLogger) Debugf(format string, args ...interface{})

func (*DefaultLogger) Error added in v1.0.2

func (l *DefaultLogger) Error(msg string, attrs ...slog.Attr)

func (*DefaultLogger) Errorf added in v1.0.2

func (l *DefaultLogger) Errorf(format string, args ...interface{})

func (*DefaultLogger) Fatal added in v1.0.2

func (l *DefaultLogger) Fatal(msg string, attrs ...slog.Attr)

func (*DefaultLogger) Fatalf added in v1.0.2

func (l *DefaultLogger) Fatalf(format string, args ...interface{})

func (*DefaultLogger) Info added in v1.0.2

func (l *DefaultLogger) Info(msg string, attrs ...slog.Attr)

func (*DefaultLogger) Infof added in v1.0.2

func (l *DefaultLogger) Infof(format string, args ...interface{})

func (*DefaultLogger) Warn added in v1.0.2

func (l *DefaultLogger) Warn(msg string, attrs ...slog.Attr)

func (*DefaultLogger) Warnf added in v1.0.2

func (l *DefaultLogger) Warnf(format string, args ...interface{})

func (*DefaultLogger) With added in v1.0.2

func (l *DefaultLogger) With(attrs ...slog.Attr) Logger

func (*DefaultLogger) WithGroup added in v1.0.2

func (l *DefaultLogger) WithGroup(name string) Logger

type ElasticsearchSink added in v1.0.2

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

ElasticsearchSink Elasticsearch输出

func NewElasticsearchSink added in v1.0.2

func NewElasticsearchSink(cfg ElasticsearchSinkConfig) *ElasticsearchSink

func (*ElasticsearchSink) Close added in v1.0.2

func (e *ElasticsearchSink) Close() error

func (*ElasticsearchSink) Write added in v1.0.2

func (e *ElasticsearchSink) Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error

type ElasticsearchSinkConfig added in v1.0.2

type ElasticsearchSinkConfig struct {
	URL     string        `json:"url" yaml:"url" toml:"url"`
	Index   string        `json:"index" yaml:"index" toml:"index"`
	Timeout time.Duration `json:"timeout" yaml:"timeout" toml:"timeout"`
}

ElasticsearchSinkConfig Elasticsearch输出配置

type FileSink added in v1.0.2

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

FileSink 文件输出

func NewFileSink added in v1.0.2

func NewFileSink(cfg FileSinkConfig) *FileSink

func (*FileSink) Close added in v1.0.2

func (f *FileSink) Close() error

func (*FileSink) Write added in v1.0.2

func (f *FileSink) Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error

type FileSinkConfig added in v1.0.2

type FileSinkConfig struct {
	Filename   string `json:"filename" yaml:"filename" toml:"filename"`
	MaxSize    int    `json:"max_size" yaml:"max_size" toml:"max_size"` // MB
	MaxBackups int    `json:"max_backups" yaml:"max_backups" toml:"max_backups"`
	MaxAge     int    `json:"max_age" yaml:"max_age" toml:"max_age"` // days
	Compress   bool   `json:"compress" yaml:"compress" toml:"compress"`
	Format     string `json:"format" yaml:"format" toml:"format"` // json, text
}

FileSinkConfig 文件输出配置

type LogConfig added in v1.0.2

type LogConfig struct {
	Level  string `json:"level" yaml:"level" toml:"level"`
	Format string `json:"format" yaml:"format" toml:"format"`

	Console ConsoleSinkConfig `json:"console" yaml:"console" toml:"console"`
	File    FileSinkConfig    `json:"file" yaml:"file" toml:"file"`

	Elasticsearch ElasticsearchSinkConfig `json:"elasticsearch" yaml:"elasticsearch" toml:"elasticsearch"`
	Loki          LokiSinkConfig          `json:"loki" yaml:"loki" toml:"loki"`
}

LogConfig 日志配置

type Logger added in v1.0.2

type Logger interface {
	Debug(msg string, attrs ...slog.Attr)
	Info(msg string, attrs ...slog.Attr)
	Warn(msg string, attrs ...slog.Attr)
	Error(msg string, attrs ...slog.Attr)
	Fatal(msg string, attrs ...slog.Attr)

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})

	With(attrs ...slog.Attr) Logger
	WithGroup(name string) Logger
}

Logger 统一的日志记录接口

func GetGlobalLogger added in v1.0.2

func GetGlobalLogger() Logger

GetGlobalLogger 获取全局日志记录器

func New

func New(cfg configs.Config) Logger

New 创建日志记录器(兼容旧接口)

func NewLogger added in v1.0.2

func NewLogger(cfg configs.Config) Logger

NewLogger 根据配置创建日志记录器

func With added in v1.0.2

func With(attrs ...slog.Attr) Logger

func WithGroup added in v1.0.2

func WithGroup(name string) Logger

type LokiSink added in v1.0.2

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

LokiSink Loki输出

func NewLokiSink added in v1.0.2

func NewLokiSink(cfg LokiSinkConfig) *LokiSink

func (*LokiSink) Close added in v1.0.2

func (l *LokiSink) Close() error

func (*LokiSink) Write added in v1.0.2

func (l *LokiSink) Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error

type LokiSinkConfig added in v1.0.2

type LokiSinkConfig struct {
	URL     string            `json:"url" yaml:"url" toml:"url"`
	Labels  map[string]string `json:"labels" yaml:"labels" toml:"labels"`
	Timeout time.Duration     `json:"timeout" yaml:"timeout" toml:"timeout"`
}

LokiSinkConfig Loki输出配置

type MultiSink added in v1.0.2

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

MultiSink 多输出目标

func NewMultiSink added in v1.0.2

func NewMultiSink(sinks ...Sink) *MultiSink

func (*MultiSink) Close added in v1.0.2

func (m *MultiSink) Close() error

func (*MultiSink) Write added in v1.0.2

func (m *MultiSink) Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error

type Sink added in v1.0.2

type Sink interface {
	Write(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) error
	Close() error
}

Sink 日志输出目标抽象

type SinkHandler added in v1.0.2

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

SinkHandler slog.Handler 实现

func (*SinkHandler) Enabled added in v1.0.2

func (h *SinkHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SinkHandler) Handle added in v1.0.2

func (h *SinkHandler) Handle(ctx context.Context, r slog.Record) error

func (*SinkHandler) WithAttrs added in v1.0.2

func (h *SinkHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SinkHandler) WithGroup added in v1.0.2

func (h *SinkHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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