Documentation
¶
Overview ¶
Package filter is a Logger that only allows entries of a given level.
Index ¶
- type LogEntry
- func (l *LogEntry) Debug() slog.Logger
- func (l *LogEntry) Enabled() bool
- func (l *LogEntry) Error() slog.Logger
- func (l *LogEntry) Fatal() slog.Logger
- func (l *LogEntry) Info() slog.Logger
- func (l *LogEntry) Level() slog.LogLevel
- func (l *LogEntry) Panic() slog.Logger
- func (l *LogEntry) Print(args ...any)
- func (l *LogEntry) Printf(format string, args ...any)
- func (l *LogEntry) Println(args ...any)
- func (l *LogEntry) Warn() slog.Logger
- func (l *LogEntry) WithEnabled() (slog.Logger, bool)
- func (l *LogEntry) WithField(label string, value any) slog.Logger
- func (l *LogEntry) WithFields(fields map[string]any) slog.Logger
- func (l *LogEntry) WithLevel(level slog.LogLevel) slog.Logger
- func (l *LogEntry) WithStack(skip int) slog.Logger
- type Logger
- func (l *Logger) Debug() slog.Logger
- func (*Logger) Enabled() bool
- func (l *Logger) Error() slog.Logger
- func (l *Logger) Fatal() slog.Logger
- func (l *Logger) Info() slog.Logger
- func (l *Logger) Panic() slog.Logger
- func (*Logger) Print(args ...any)
- func (*Logger) Printf(_ string, args ...any)
- func (*Logger) Println(args ...any)
- func (l *Logger) Warn() slog.Logger
- func (l *Logger) WithEnabled() (slog.Logger, bool)
- func (l *Logger) WithField(label string, value any) slog.Logger
- func (l *Logger) WithFields(fields map[string]any) slog.Logger
- func (l *Logger) WithLevel(level slog.LogLevel) slog.Logger
- func (l *Logger) WithStack(skip int) slog.Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LogEntry ¶
type LogEntry struct {
// contains filtered or unexported fields
}
LogEntry implements a level-filtered logger.
func (*LogEntry) Level ¶ added in v0.7.0
Level returns the current log level. Exposed for testing only.
func (*LogEntry) Print ¶
Print would, if conditions are met, add a log entry with the arguments in the manner of fmt.Print.
func (*LogEntry) Printf ¶
Printf would, if conditions are met, add a log entry with the arguments in the manner of fmt.Printf.
func (*LogEntry) Println ¶
Println would, if conditions are met, add a log entry with the arguments in the manner of fmt.Println.
func (*LogEntry) WithEnabled ¶
WithEnabled returns itself and whether it's enabled.
func (*LogEntry) WithField ¶
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 ¶
WithFields would, if conditions are met, attach fields to the log entry. These fields could be altered if a FieldFilter is used.
type Logger ¶
type Logger struct {
// Parent is the Logger to use 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 single fields before passing them
// to the Parent logger.
FieldFilter func(key string, val any) (string, any, bool)
// FieldsFilter allows us to modify field maps before passing them
// to the Parent logger. Returns filtered fields and whether to continue.
FieldsFilter func(fields slog.Fields) (slog.Fields, bool)
// MessageFilter allows us to modify Print() messages before passing
// them to the Parent logger, or completely discard the entry.
MessageFilter func(msg string) (string, bool)
// contains filtered or unexported fields
}
Logger implements a factory for level-filtered loggers.
func New ¶
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() *Logger
NewNoop creates a new filtered log factory that only implements Fatal().Print().
func (*Logger) WithEnabled ¶
WithEnabled tells this logger doesn't log anything, but WithLevel() might.
func (*Logger) WithFields ¶
WithFields creates a LogEntry with the fields.