filter

package module
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 5 Imported by: 5

README

Filtered Logger for slog.Logger

Go Reference

See also

Documentation

Overview

Package filter is a Logger that only allows entries of a given level

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(parent slog.Logger, threshold slog.LogLevel) slog.Logger

New creates a new filtered log factory at a given level. Logger can be manually initialised as well. Defaults filter entries at level slog.Error or higher Parentless is treated as `noop`, with Fatal implemented like log.Fatal

func NewNoop

func NewNoop() slog.Logger

NewNoop creates a new filtered log factory that only implements Fatal().Print()

Types

type LogEntry

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

LogEntry implements a level filtered logger

func (*LogEntry) Debug

func (l *LogEntry) Debug() slog.Logger

Debug creates a new filtered logger on level slog.Debug

func (*LogEntry) Enabled

func (l *LogEntry) Enabled() bool

Enabled tells this logger would record logs

func (*LogEntry) Error

func (l *LogEntry) Error() slog.Logger

Error creates a new filtered logger on level slog.Error

func (*LogEntry) Fatal

func (l *LogEntry) Fatal() slog.Logger

Fatal creates a new filtered logger on level slog.Fatal

func (*LogEntry) Info

func (l *LogEntry) Info() slog.Logger

Info creates a new filtered logger on level slog.Info

func (*LogEntry) Panic

func (l *LogEntry) Panic() slog.Logger

Panic creates a new filtered logger on level slog.Panic

func (*LogEntry) Print

func (l *LogEntry) Print(args ...any)

Print would, if conditions are met, add a log entry with the arguments in the manner of fmt.Print

func (*LogEntry) Printf

func (l *LogEntry) Printf(format string, args ...any)

Printf would, if conditions are met, add a log entry with the arguments in the manner of fmt.Printf

func (*LogEntry) Println

func (l *LogEntry) Println(args ...any)

Println would, if conditions are met, add a log entry with the arguments in the manner of fmt.Println

func (*LogEntry) Warn

func (l *LogEntry) Warn() slog.Logger

Warn creates a new filtered logger on level slog.Warn

func (*LogEntry) WithEnabled

func (l *LogEntry) WithEnabled() (slog.Logger, bool)

WithEnabled returns itself and if it's enabled

func (*LogEntry) WithField

func (l *LogEntry) WithField(label string, value any) slog.Logger

WithField would, if conditions are met, attach a field to the log entry. This field could be altered if a FieldFilter is used

func (*LogEntry) WithFields

func (l *LogEntry) WithFields(fields map[string]any) slog.Logger

WithFields would, if conditions are met, attach fields to the log entry. These fields could be altered if a FieldFilter is used

func (*LogEntry) WithLevel

func (l *LogEntry) WithLevel(level slog.LogLevel) slog.Logger

WithLevel creates a new filtered logger on the given level

func (*LogEntry) WithStack

func (l *LogEntry) WithStack(skip int) slog.Logger

WithStack would, if conditions are met, attach a call stack to the log entry

type Logger

type Logger struct {
	// Parent is the Logger to used as backend when conditions are met
	Parent slog.Logger

	// Threshold is the minimum level to be logged
	Threshold slog.LogLevel

	// FieldFilter allows us to modify filters before passing them
	// to the Parent logger
	FieldFilter func(key string, val any) (string, any, bool)

	// FieldOverride intercepts calls to WithField() on enabled loggers
	// to let you transform the field
	FieldOverride func(entry slog.Logger, key string, val any)

	// FieldsOverride intercepts calls to WithFields() on enabled loggers
	// to let you transform the fields
	FieldsOverride func(entry slog.Logger, fields map[string]any)

	// MessageFilter allows us to modify Print() messages before passing
	// them to the Parent logger, on completely discard the entry
	MessageFilter func(msg string) (string, bool)
}

Logger implements a factory for level filtered loggers

func (*Logger) Debug

func (l *Logger) Debug() slog.Logger

Debug returns a filtered logger on level slog.Debug

func (*Logger) Enabled

func (*Logger) Enabled() bool

Enabled tells this logger doesn't log anything, but WithLevel() might

func (*Logger) Error

func (l *Logger) Error() slog.Logger

Error returns a filtered logger on level slog.Error

func (*Logger) Fatal

func (l *Logger) Fatal() slog.Logger

Fatal returns a filtered logger on level slog.Fatal

func (*Logger) Info

func (l *Logger) Info() slog.Logger

Info returns a filtered logger on level slog.Info

func (*Logger) Panic

func (l *Logger) Panic() slog.Logger

Panic returns a filtered logger on level slog.Panic

func (*Logger) Print

func (*Logger) Print(...any)

Print does nothing

func (*Logger) Printf

func (*Logger) Printf(string, ...any)

Printf does nothing

func (*Logger) Println

func (*Logger) Println(...any)

Println does nothing

func (*Logger) Warn

func (l *Logger) Warn() slog.Logger

Warn returns a filtered logger on level slog.Warn

func (*Logger) WithEnabled

func (l *Logger) WithEnabled() (slog.Logger, bool)

WithEnabled tells this logger doesn't log anything, but WithLevel() might

func (*Logger) WithField

func (l *Logger) WithField(string, any) slog.Logger

WithField does nothing

func (*Logger) WithFields

func (l *Logger) WithFields(map[string]any) slog.Logger

WithFields does nothing

func (*Logger) WithLevel

func (l *Logger) WithLevel(level slog.LogLevel) slog.Logger

WithLevel returns a filtered logger set to the given level

func (*Logger) WithStack

func (l *Logger) WithStack(int) slog.Logger

WithStack does nothing

Jump to

Keyboard shortcuts

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