Realization of slog handlers for filtering logs and coloring logs
Defining the slog.Logger -> integrate handler
handler := slogslav.NewFilterTextHandler(os.Stdout, nil, nil)
logger := slog.New(handler)
// common logger using
Interface:
func NewFilterTextHandler(w io.Writer, opts *slog.HandlerOptions, level slog.Level) *FilterHandler {}
func NewFilterJSONHandler(w io.Writer, opts *slog.HandlerOptions, level slog.Level) *FilterHandler {}
func NewColorfulTextHandler(out io.Writer, formatter ColorFormatter, handlerOpts *slog.HandlerOptions) *ColorfulHandler {}
type ColorFormatter interface {
Err(level slog.Level) string
Warn(level slog.Level) string
Debug(level slog.Level) string
Info(level slog.Level) string
Time(logtime string) string
Separator() string
}
Err
, Warn
, Debug
, Info
, Time
, Separator
- are used in writing into io.Writer interface. Anything that implements this interface may be used as ColorFormatter
The default implementation is ColorFormatterPalette
type ColorFormatterPalette struct {
err *color.Color
warn *color.Color
debug *color.Color
info *color.Color
time *color.Color
separator byte
}
It uses fatih/color
Described in slog manuals