log

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package log provides structured logging with multiple sink support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

func Debugf

func Debugf(format string, args ...any)

func Error

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

func Errorf

func Errorf(format string, args ...any)

func Fatal

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

func Fatalf

func Fatalf(format string, args ...any)

func Info

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

func Infof

func Infof(format string, args ...any)

func SetGlobalLogger

func SetGlobalLogger(logger Logger)

SetGlobalLogger sets the global logger instance.

func Warn

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

func Warnf

func Warnf(format string, args ...any)

Types

type ConsoleSink

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

ConsoleSink outputs logs to console with formatting options.

func NewConsoleSink

func NewConsoleSink(cfg ConsoleSinkConfig) *ConsoleSink

NewConsoleSink creates a console sink.

func (*ConsoleSink) Close

func (c *ConsoleSink) Close() error

func (*ConsoleSink) Write

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

type ConsoleSinkConfig

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 configuration for console output.

type DefaultLogger

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

DefaultLogger is the default logger implementation.

func NewDefaultLogger

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

NewDefaultLogger creates a logger with the given sink and level.

func (*DefaultLogger) Debug

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

func (*DefaultLogger) Debugf

func (l *DefaultLogger) Debugf(format string, args ...any)

func (*DefaultLogger) Error

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

func (*DefaultLogger) Errorf

func (l *DefaultLogger) Errorf(format string, args ...any)

func (*DefaultLogger) Fatal

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

func (*DefaultLogger) Fatalf

func (l *DefaultLogger) Fatalf(format string, args ...any)

func (*DefaultLogger) Info

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

func (*DefaultLogger) Infof

func (l *DefaultLogger) Infof(format string, args ...any)

func (*DefaultLogger) Warn

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

func (*DefaultLogger) Warnf

func (l *DefaultLogger) Warnf(format string, args ...any)

func (*DefaultLogger) With

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

func (*DefaultLogger) WithGroup

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

type ElasticsearchSink

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

ElasticsearchSink outputs logs to Elasticsearch.

func NewElasticsearchSink

func NewElasticsearchSink(cfg ElasticsearchSinkConfig) *ElasticsearchSink

NewElasticsearchSink creates an Elasticsearch sink.

func (*ElasticsearchSink) Close

func (e *ElasticsearchSink) Close() error

func (*ElasticsearchSink) Write

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

type ElasticsearchSinkConfig

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 configuration for Elasticsearch output.

type FileSink

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

FileSink outputs logs to a file with rotation support.

func NewFileSink

func NewFileSink(cfg FileSinkConfig) *FileSink

NewFileSink creates a file sink.

func (*FileSink) Close

func (f *FileSink) Close() error

func (*FileSink) Write

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

type FileSinkConfig

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"` // not implemented in simple version
	MaxAge     int    `json:"max_age" yaml:"max_age" toml:"max_age"`             // not implemented in simple version
	Compress   bool   `json:"compress" yaml:"compress" toml:"compress"`          // not implemented in simple version
	Format     string `json:"format" yaml:"format" toml:"format"`                // json, text
}

FileSinkConfig configuration for file output.

type LogConfig

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 extended configuration for logging.

type Logger

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 ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)

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

Logger is the unified logging interface.

func GetGlobalLogger

func GetGlobalLogger() Logger

GetGlobalLogger returns the global logger instance.

func New

func New(cfg config.LogConfig) Logger

New creates a logger from the base configuration.

func NewFromLogConfig

func NewFromLogConfig(cfg LogConfig, env string) Logger

NewFromLogConfig creates a logger from extended config with multiple sinks.

func With

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

func WithGroup

func WithGroup(name string) Logger

type LokiSink

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

LokiSink outputs logs to Grafana Loki.

func NewLokiSink

func NewLokiSink(cfg LokiSinkConfig) *LokiSink

NewLokiSink creates a Loki sink.

func (*LokiSink) Close

func (l *LokiSink) Close() error

func (*LokiSink) Write

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

type LokiSinkConfig

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 configuration for Loki output.

type MultiSink

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

MultiSink writes to multiple sinks.

func NewMultiSink

func NewMultiSink(sinks ...Sink) *MultiSink

NewMultiSink creates a sink that writes to multiple targets.

func (*MultiSink) Close

func (m *MultiSink) Close() error

func (*MultiSink) Write

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

type Sink

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

Sink is the log output target abstraction.

type SinkHandler

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

SinkHandler implements slog.Handler.

func (*SinkHandler) Enabled

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

func (*SinkHandler) Handle

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

func (*SinkHandler) WithAttrs

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

func (*SinkHandler) WithGroup

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