flexlog

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: BSD-3-Clause Imports: 13 Imported by: 0

README

flexlog

The Flexible slog.Handler

About

I wanted to switch from logrus to slog in aws-sso-cli, but I found the default slog library a bit too basic for my tastes. Additionally, one feature I really <3 about logrus was the ability to write unit tests against generated logs; a feature which is currently missing from slog.

Anyways, I decided to breakout my work into a separate library for future use. Maybe it'll work for you too?

Documentation

Index

Constants

View Source
const (
	LevelTrace  = slog.Level(-8)
	LevelFatal  = slog.Level(12)
	StackFrames = 5 // number of stack frames to skip in Handler.Handle
)

Define our custom levels

View Source
const (
	FrameMarker = "__skip_frames"
)

Variables

View Source
var LevelColorsMap map[slog.Level]LevelColor = map[slog.Level]LevelColor{
	LevelTrace:      {Name: "TRACE", Color: color.FgGreen},
	LevelFatal:      {Name: "FATAL", Color: color.FgRed},
	slog.LevelInfo:  {Name: "INFO ", Color: color.FgBlue},
	slog.LevelWarn:  {Name: "WARN ", Color: color.FgYellow},
	slog.LevelError: {Name: "ERROR", Color: color.FgRed},
	slog.LevelDebug: {Name: "DEBUG", Color: color.FgMagenta},
}

LevelColorsMap is a map of log levels to their colors and the name of the level.

View Source
var LevelNames = map[slog.Leveler]string{
	LevelTrace: "TRACE",
	LevelFatal: "FATAL",
}

LevelNames defines additional log levels

View Source
var LevelStrings = map[string]slog.Level{
	"TRACE": LevelTrace,
	"FATAL": LevelFatal,
	"INFO":  slog.LevelInfo,
	"WARN":  slog.LevelWarn,
	"ERROR": slog.LevelError,
	"DEBUG": slog.LevelDebug,
}

LevelStrings is a map of level names to their slog.Level values.

Functions

func NewConsole

func NewConsole(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)

NewConsole creates a new slog.Handler for the ConsoleHandler, which wraps tint.NewHandler with some customizations.

func NewConsoleHandler

func NewConsoleHandler(w io.Writer, opts *tint.Options) slog.Handler

ConsoleHandler is a slog.Handler that wraps tint.Handler

func NewJSON

func NewJSON(w io.Writer, addSource bool, level slog.Leveler, _ bool) (slog.Handler, *slog.LevelVar)

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *slogjson.HandlerOptions) slog.Handler

func NewTint

func NewTint(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)

Types

type ConsoleHandler

type ConsoleHandler struct {
	slog.Handler
}

implement the slog.Handler interface via the tint.Handler

func (*ConsoleHandler) Handle

func (h *ConsoleHandler) Handle(ctx context.Context, r slog.Record) error

Handle is a custom wrapper around the tint.Handler.Handle method which fixes up the PC value to be the correct caller for the Fatal/Trace methods

type FlexLogger

type FlexLogger interface {
	// slog.Logger methods
	Debug(msg string, args ...any)
	DebugContext(ctx context.Context, msg string, args ...any)
	Enabled(ctx context.Context, level slog.Level) bool
	Error(msg string, args ...any)
	ErrorContext(ctx context.Context, msg string, args ...any)
	Handler() slog.Handler
	Info(msg string, args ...any)
	InfoContext(ctx context.Context, msg string, args ...any)
	Log(ctx context.Context, level slog.Level, msg string, args ...any)
	LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
	Warn(msg string, args ...any)
	WarnContext(ctx context.Context, msg string, args ...any)
	With(args ...any) *slog.Logger
	WithGroup(name string) *slog.Logger
	// custom methods
	Copy() FlexLogger
	GetLevel() slog.Level
	GetLogger() *slog.Logger
	SetLevel(level slog.Leveler)
	SetLevelString(level string) error
	SetLogger(logger *slog.Logger)
	SetReportCaller(reportCaller bool)
	Trace(msg string, args ...any)
	TraceContext(ctx context.Context, msg string, args ...any)
	Fatal(msg string, args ...any)
	FatalContext(ctx context.Context, msg string, args ...any)
	Writer() io.Writer
	AddSource() bool
	Level() *slog.LevelVar
	Color() bool
}

type JsonHandler

type JsonHandler struct {
	slog.Handler
}

func (*JsonHandler) Handle

func (h *JsonHandler) Handle(ctx context.Context, r slog.Record) error

Handle is a custom wrapper around the slogjson.Handler.Handle method which fixes up the PC value to be the correct caller for the Fatal/Trace methods

type LevelColor

type LevelColor struct {
	// Name is the name of the log level
	Name string
	// Color is the color of the log level
	Color color.Attribute
	// contains filtered or unexported fields
}

LevelColors defines the name as displayed to the user and color of a log level.

func (*LevelColor) Copy

func (lc *LevelColor) Copy() *LevelColor

Copy returns a copy of the LevelColor.

func (*LevelColor) String

func (lc *LevelColor) String(colored bool) string

String returns the level name, optionally with color applied.

type LevelColors

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

LevelColors is our internal representation of the user-defined LevelColorsMapping. We map the log levels via their slog.Level to their LevelColor using an offset to ensure we can map negative level values to our slice.

func (*LevelColors) Copy

func (lc *LevelColors) Copy() *LevelColors

Copy returns a copy of the LevelColors.

func (*LevelColors) LevelColor

func (lc *LevelColors) LevelColor(level slog.Level) *LevelColor

LevelColor returns the LevelColor for the given log level. Returns nil indicating if the log level was not found.

type LevelColorsMapping

type LevelColorsMapping map[slog.Level]LevelColor

LevelColorsMapping is a map of log levels to their colors and is what the user defines in their configuration.

func (*LevelColorsMapping) LevelColors

func (lm *LevelColorsMapping) LevelColors() *LevelColors

LevelColors returns the LevelColors for the LevelColorsMapping.

type Logger

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

Our logger which wraps slog.Logger and implements CustomLogger

func NewLogger

func NewLogger(f NewLoggerFunc, w io.Writer, addSource bool, level slog.Leveler, color bool) *Logger

NewLoggerFunc creates a new Logger

func (*Logger) AddSource

func (l *Logger) AddSource() bool

AddSource returns whether the logger is configured to include the source file and line number in the log output

func (*Logger) Color

func (l *Logger) Color() bool

Color returns whether the logger is configured to output colorized log messages

func (*Logger) Copy

func (l *Logger) Copy() FlexLogger

Copy returns a copy of the Logger current Logger

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs a message at the debug level

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a message at the debug level with context

func (*Logger) Enabled

func (l *Logger) Enabled(ctx context.Context, level slog.Level) bool

Enabled returns whether the logger is enabled for the specified level

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs a message at the error level

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs a message at the error level with context

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...interface{})

Log a message at the Fatal level and exit

func (*Logger) FatalContext

func (l *Logger) FatalContext(ctx context.Context, msg string, args ...interface{})

Log a message at the Fatal level with context and exit

func (*Logger) GetLevel

func (l *Logger) GetLevel() slog.Level

GetLevel returns the current log level

func (*Logger) GetLogger

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

GetLogger returns the underlying slog.Logger

func (*Logger) Handler

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

Handler returns the slog.Handler for the logger

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs a message at the info level

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs a message at the info level with context

func (*Logger) Level

func (l *Logger) Level() *slog.LevelVar

Level returns the current log level

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log logs a message at the specified level with the included context

func (*Logger) LogAttrs

func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs logs a message at the specified level with the included context and attributes

func (*Logger) LogWithSource

func (l *Logger) LogWithSource(ctx context.Context, level slog.Level, frames int, msg string, args ...interface{})

LogWithSource sets the __source attribute so that our Handler knows to modify the r.PC value to include the original caller.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level slog.Leveler)

SetLevel sets the log level for the logger

func (*Logger) SetLevelString

func (l *Logger) SetLevelString(level string) error

SetLevelString sets the log level for the logger by string

func (*Logger) SetLogger

func (l *Logger) SetLogger(logger *slog.Logger)

SetLogger sets the underlying slog.Logger

func (*Logger) SetReportCaller

func (l *Logger) SetReportCaller(reportCaller bool)

SetReportCaller sets whether to include the source file and line number in the log output Doing so will replace the current logger with a new one that has the new setting

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...interface{})

Log a message at the Trace level

func (*Logger) TraceContext

func (l *Logger) TraceContext(ctx context.Context, msg string, args ...interface{})

Log a message at the Trace level with context

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs a message at the warn level

func (*Logger) WarnContext

func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs a message at the warn level with context

func (*Logger) With

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

With returns a new logger with the specified attributes

func (*Logger) WithGroup

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

WithGroup returns a new logger with the specified group name

func (*Logger) Writer

func (l *Logger) Writer() io.Writer

Writer returns the writer for the logger

type NewLoggerFunc

type NewLoggerFunc func(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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