logger

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package logger provides interfaces for logging with various levels of verbosity and functionality. It also provides a convenient (but experimental) way of migrating from logrus to slog.

License: MIT Copyright: 2023, Denis Voytyuk

Package logger.

License: MIT Copyright: 2023, Denis Voytyuk

Index

Constants

View Source
const SlogLevelFatal slog.Level = 100

Variables

This section is empty.

Functions

func Debug added in v1.1.0

func Debug(args ...any)

func Debugf added in v1.1.0

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

func Error added in v1.1.0

func Error(args ...any)

func Errorf added in v1.1.0

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

func Fatal added in v1.1.0

func Fatal(args ...any)

func Fatalf added in v1.1.0

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

func Info added in v1.1.0

func Info(args ...any)

func Infof added in v1.1.0

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

func Panic added in v1.1.0

func Panic(args ...any)

func Panicf added in v1.1.0

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

func Print added in v1.1.0

func Print(args ...any)

func Printf added in v1.1.0

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

func SlogFields added in v1.1.0

func SlogFields(args ...any) []any

SlogFields is a helper function that converts a list of key-value pairs to a slice of them. Use this function in conjunction with WithFields to pass a slice of key-value pairs to it.

func Warn added in v1.1.0

func Warn(args ...any)

func Warnf added in v1.1.0

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

func Warning added in v1.1.0

func Warning(args ...any)

func Warningf added in v1.1.0

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

Types

type BasicLogger

type BasicLogger interface {
	PrimitiveLogger

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

	Fatal(args ...any)
	Panic(args ...any)
}

BasicLogger is a logger interface that extends PrimitiveLogger and provides additional methods for logging fatal errors and panics.

type FieldLogger

type FieldLogger[T any, U any] interface {
	LevelLogger

	WithField(key string, value any) U
	WithFields(fields T) U
	WithError(err error) U
}

FieldLogger is a logger interface that extends LevelLogger and provides additional methods for logging messages with structured data. For use with logrus, the T and U type parameters should be set to logrus.Fields and *logrus.Entry respectively.

type LevelLogger

type LevelLogger interface {
	BasicLogger

	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Warningf(format string, args ...any)
	Errorf(format string, args ...any)

	Debug(args ...any)
	Info(args ...any)
	Warn(args ...any)
	Warning(args ...any)
	Error(args ...any)
}

LevelLogger is a logger interface that extends BasicLogger and provides additional methods for logging messages with various levels of severity.

type PrimitiveLogger

type PrimitiveLogger interface {
	Printf(format string, args ...any)
	Print(args ...any)
}

PrimitiveLogger is the simplest logger interface that only provides the Printf and Print methods.

type Slog added in v1.1.0

type Slog struct {
	*slog.Logger
}

Slog is a wrapper around slog.Logger that implements FieldLogger. It is intended to be used as a logrus migration path. It is not intended to be used as a general purpose logger. Note, this is an experimental approach and it's not recommended to be used in production.

To migrate from logrus do the following steps:

  1. Replace all logrus implementation references with logger.FieldLogger (or any other suitable interface from this package)
  2. Replace all logrus.New() calls with logger.NewSlog(slog.New())
  3. Replace all calls to logrus.* with logger.* (e.g. logrus.WithField -> logger.WithField)
  4. You will have to manually adjust remaining incopatibilities (e.g. this package does not support logrus.Fields and they have to be replaced with logger.SlogFields).

As this struct is a wrapper around slog.Logger, it is possible to use slog.Logger methods.

func NewSlog added in v1.1.0

func NewSlog(logger *slog.Logger) *Slog

func WithError added in v1.1.0

func WithError(err error) *Slog

func WithField added in v1.1.0

func WithField(key string, value any) *Slog

func WithFields added in v1.1.0

func WithFields(fields []any) *Slog

func (*Slog) Debug added in v1.1.0

func (s *Slog) Debug(args ...any)

func (*Slog) Debugf added in v1.1.0

func (s *Slog) Debugf(format string, args ...any)

func (*Slog) Error added in v1.1.0

func (s *Slog) Error(args ...any)

func (*Slog) Errorf added in v1.1.0

func (s *Slog) Errorf(format string, args ...any)

func (*Slog) Fatal added in v1.1.0

func (s *Slog) Fatal(args ...any)

func (*Slog) Fatalf added in v1.1.0

func (s *Slog) Fatalf(format string, args ...any)

func (*Slog) Info added in v1.1.0

func (s *Slog) Info(args ...any)

func (*Slog) Infof added in v1.1.0

func (s *Slog) Infof(format string, args ...any)

func (*Slog) Panic added in v1.1.0

func (s *Slog) Panic(args ...any)

func (*Slog) Panicf added in v1.1.0

func (s *Slog) Panicf(format string, args ...any)

func (*Slog) Print added in v1.1.0

func (s *Slog) Print(args ...any)

func (*Slog) Printf added in v1.1.0

func (s *Slog) Printf(format string, args ...any)

func (*Slog) Warn added in v1.1.0

func (s *Slog) Warn(args ...any)

func (*Slog) Warnf added in v1.1.0

func (s *Slog) Warnf(format string, args ...any)

func (*Slog) Warning added in v1.1.0

func (s *Slog) Warning(args ...any)

func (*Slog) Warningf added in v1.1.0

func (s *Slog) Warningf(format string, args ...any)

func (*Slog) WithError added in v1.1.0

func (s *Slog) WithError(err error) *Slog

func (*Slog) WithField added in v1.1.0

func (s *Slog) WithField(key string, value any) *Slog

func (*Slog) WithFields added in v1.1.0

func (s *Slog) WithFields(fields []any) *Slog

type TraceFieldLogger

type TraceFieldLogger[T any, U any] interface {
	TraceLogger
	FieldLogger[T, U]
}

TraceFieldLogger extends TraceLogger and FieldLogger with methods for adding fields to trace messages. For use with logrus, the T and U type parameters should be set to logrus.Fields and *logrus.Entry respectively.

type TraceLogger

type TraceLogger interface {
	LevelLogger

	Tracef(format string, args ...any)
	Trace(args ...any)
}

TraceLogger is a logger interface that extends LevelLogger and provides additional methods for logging messages with the lowest level of severity.

Jump to

Keyboard shortcuts

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