logging

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 10 Imported by: 1

README

Slog based logging for Go

This package is almost a drop in replacement for logrus. It's based on slog logger which is now part of Go standard library.

Features

  • Rate limit
  • Export hook for logs export to external systems
  • Logfmt text format handler with source lines support.

Install

go get github.com/castai/logging

Example

See logging_test.go example test.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParseLevel

func MustParseLevel(lvlStr string) slog.Level

func PrintDroppedLogs

func PrintDroppedLogs(ctx context.Context, interval time.Duration, r *RateLimitHandler, printFunc func(level slog.Level, count uint64))

PrintDroppedLogs prints dropped rate limit logs and resets counter to 0.

Types

type ExportHandler

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

ExportHandler exports logs to separate channel available via Records()

func NewExportHandler

func NewExportHandler(cfg ExportHandlerConfig) *ExportHandler

func (*ExportHandler) Enabled

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

func (*ExportHandler) Handle

func (h *ExportHandler) Handle(ctx context.Context, record slog.Record) error

func (*ExportHandler) Records

func (h *ExportHandler) Records() <-chan slog.Record

func (*ExportHandler) Register

func (h *ExportHandler) Register(next slog.Handler) slog.Handler

func (*ExportHandler) WithAttrs

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

func (*ExportHandler) WithGroup

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

type ExportHandlerConfig

type ExportHandlerConfig struct {
	MinLevel   slog.Level // Only export logs for this min log level.
	BufferSize int        // Logs channel size.
}

type Handler

type Handler interface {
	Register(next slog.Handler) slog.Handler
}

Handler allows to chain multiple handlers. Order of execution is reverse to order of registration meaning first handler is executed last.

func NewTextHandler

func NewTextHandler(cfg TextHandlerConfig) Handler

NewTextHandler returns slog text handler.

type HandlerFunc

type HandlerFunc func(next slog.Handler) slog.Handler

func (HandlerFunc) Register

func (h HandlerFunc) Register(next slog.Handler) slog.Handler

type Logger

type Logger struct {
	Log *slog.Logger
}

Logger is a small wrapper around slog with some extra methods for easier migration from logrus.

func New

func New(handlers ...Handler) *Logger

func (*Logger) Debug

func (l *Logger) Debug(msg string)

func (*Logger) Debugf

func (l *Logger) Debugf(format string, a ...any)

func (*Logger) Error

func (l *Logger) Error(msg string)

func (*Logger) Errorf

func (l *Logger) Errorf(format string, a ...any)

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

func (*Logger) Fatalf added in v0.2.0

func (l *Logger) Fatalf(msg string, a ...any)

func (*Logger) Info

func (l *Logger) Info(msg string)

func (*Logger) Infof

func (l *Logger) Infof(format string, a ...any)

func (*Logger) IsEnabled

func (l *Logger) IsEnabled(lvl slog.Level) bool

func (*Logger) Warn

func (l *Logger) Warn(msg string)

func (*Logger) Warnf

func (l *Logger) Warnf(format string, a ...any)

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

func (*Logger) WithField

func (l *Logger) WithField(k, v string) *Logger

func (*Logger) WithGroup

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

type RateLimitHandler

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

func NewRateLimitHandler

func NewRateLimitHandler(cfg RateLimiterHandlerConfig) *RateLimitHandler

func (*RateLimitHandler) Enabled

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

func (*RateLimitHandler) Handle

func (h *RateLimitHandler) Handle(ctx context.Context, record slog.Record) error

func (*RateLimitHandler) Register

func (h *RateLimitHandler) Register(next slog.Handler) slog.Handler

func (*RateLimitHandler) WithAttrs

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

func (*RateLimitHandler) WithGroup

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

type RateLimiterHandlerConfig

type RateLimiterHandlerConfig struct {
	Limit rate.Limit
	Burst int
}

type TextHandlerConfig

type TextHandlerConfig struct {
	Level     slog.Level
	Output    io.Writer
	AddSource bool
}

Jump to

Keyboard shortcuts

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