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
- func Debug(args ...any)
- func Debugf(format string, args ...any)
- func Error(args ...any)
- func Errorf(format string, args ...any)
- func Fatal(args ...any)
- func Fatalf(format string, args ...any)
- func Info(args ...any)
- func Infof(format string, args ...any)
- func Panic(args ...any)
- func Panicf(format string, args ...any)
- func Print(args ...any)
- func Printf(format string, args ...any)
- func SlogFields(args ...any) []any
- func Warn(args ...any)
- func Warnf(format string, args ...any)
- func Warning(args ...any)
- func Warningf(format string, args ...any)
- type BasicLogger
- type FieldLogger
- type LevelLogger
- type PrimitiveLogger
- type Slog
- func (s *Slog) Debug(args ...any)
- func (s *Slog) Debugf(format string, args ...any)
- func (s *Slog) Error(args ...any)
- func (s *Slog) Errorf(format string, args ...any)
- func (s *Slog) Fatal(args ...any)
- func (s *Slog) Fatalf(format string, args ...any)
- func (s *Slog) Info(args ...any)
- func (s *Slog) Infof(format string, args ...any)
- func (s *Slog) Panic(args ...any)
- func (s *Slog) Panicf(format string, args ...any)
- func (s *Slog) Print(args ...any)
- func (s *Slog) Printf(format string, args ...any)
- func (s *Slog) Warn(args ...any)
- func (s *Slog) Warnf(format string, args ...any)
- func (s *Slog) Warning(args ...any)
- func (s *Slog) Warningf(format string, args ...any)
- func (s *Slog) WithError(err error) *Slog
- func (s *Slog) WithField(key string, value any) *Slog
- func (s *Slog) WithFields(fields []any) *Slog
- type TraceFieldLogger
- type TraceLogger
Constants ¶
const SlogLevelFatal slog.Level = 100
Variables ¶
This section is empty.
Functions ¶
func SlogFields ¶ added in v1.1.0
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.
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 ¶
PrimitiveLogger is the simplest logger interface that only provides the Printf and Print methods.
type Slog ¶ added in v1.1.0
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:
- Replace all logrus implementation references with logger.FieldLogger (or any other suitable interface from this package)
- Replace all logrus.New() calls with logger.NewSlog(slog.New())
- Replace all calls to logrus.* with logger.* (e.g. logrus.WithField -> logger.WithField)
- 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 WithFields ¶ added in v1.1.0
func (*Slog) WithFields ¶ added in v1.1.0
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.