gocliawesomeness

package module
v0.0.0-...-b56d3fc Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT Imports: 15 Imported by: 1

README

Awesome Go Slog Formatter

This is a mash up of the following to project with a bit of my own sauce:

Usage

See example/

options := &gocliawesomeness.Options{
    Format:     gocliawesomeness.Format(config.logFormat), // text or json
    Level:      gocliawesomeness.Level(config.logLevel), // debug, info, warn, err
    PrefixKeys: []string{"app", "service"},
}
slog.SetDefault(gocliawesomeness.NewAwesomeLogger(options))

logger := slog.With("app", "my-app")

Documentation

Overview

Original implementation: https://github.com/dpotapov/slogpfx

Original implementation: https://github.com/lmittmann/tint

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizePrefix

func ColorizePrefix(f func(prefixes []slog.Value) string) func(prefixes []slog.Value) string

ColorizePrefix wraps a prefix formatter function to colorize its output with cyan ANSI codes.

func DefaultPrefixFormatter

func DefaultPrefixFormatter(prefixes []slog.Value) string

DefaultPrefixFormatter constructs a prefix string by joining all detected prefix values using ":". A " > " suffix is added at the end of the prefix string.

func Err

func Err(err error) slog.Attr

Err returns a tinted (colorized) slog.Attr that will be written in red color by the [tint.Handler]. When used with any other slog.Handler, it behaves as

slog.Any("err", err)

func Level

func Level(level string) slog.Level

func NewAwesomeJsonLogger

func NewAwesomeJsonLogger(options *Options) *slog.Logger

func NewAwesomeLogger

func NewAwesomeLogger(options *Options) *slog.Logger

func NewAwesomeTextLogger

func NewAwesomeTextLogger(options *Options) *slog.Logger

func NewTintedHandler

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

NewTintedHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.

Types

type Handler

type Handler struct {
	Next slog.Handler // The next log tintedHandler in the chain.
	// contains filtered or unexported fields
}

Handler is a custom slog tintedHandler that wraps another slog.Handler to prepend a prefix to the log messages. The prefix is sourced from the log record's attributes using the keys specified in PrefixKeys.

func NewPrefixedHandler

func NewPrefixedHandler(next slog.Handler, opts *HandlerOptions) *Handler

NewTintedHandler creates a new prefix logging tintedHandler. The new tintedHandler will prepend a prefix sourced from the log record's attributes to each log message before passing the record to the next tintedHandler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

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

Handle processes a log record, prepending a prefix to its message if needed, and then passes the record to the next tintedHandler.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions

type HandlerOptions struct {
	PrefixKeys []string // A list of keys used to fetch prefix values from the log record.

	// PrefixFormatter is a function to format the prefix of the log record.
	// If it's not set, the DefaultPrefixFormatter with ColorizePrefix wrapper is used.
	PrefixFormatter func(prefixes []slog.Value) string
}

type LogFormat

type LogFormat string
const (
	FormatText LogFormat = "text"
	FormatJson LogFormat = "json"
	FormatAuto LogFormat = "auto"
)

func Format

func Format(format string) LogFormat

type Options

type Options struct {
	Format LogFormat

	// Enable source code location (Default: false)
	AddSource bool

	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See https://pkg.go.dev/log/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

	PrefixKeys []string

	// Time format (Default: time.StampMilli)
	TimeFormat string

	// Disable color (Default: false)
	NoColor bool
}

Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.

Options can be used as a drop-in replacement for slog.HandlerOptions.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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