prettylog

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

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

Go to latest
Published: Oct 12, 2024 License: MIT Imports: 7 Imported by: 0

README

Pretty Log

Implementing the slog.Handler in Go

Example

func main() {
	slog.SetDefault(slog.New(NewHandler(nil)))

	slog.Info("You did it!", slog.Group(
		"user",
		"name", "foo",
		"drink": "water",
	))

	slog.Warn("No! This will destroy you!", slog.Group(
		"user",
		"name", "bar",
		"drink": "vermouth",
	))

	slog.Error("I TOLD YOU NOT TO DO IT", slog.Group(
		"user",
		"name", "baz",
		"drink": "dizzy drinks",
	))
}

Override

func main() {
	slog.SetDefault(slog.New(NewHandler(&slog.HandlerOptions{
		AddSource: true, // to show the source file, line, etc
		Level:     slog.LevelDebug, // could be Info, Warn, Error, etc
	})))

	slog.Info("You did it!", slog.Group(
		"user",
		"name", "foo",
		"drink": "water",
	))

	slog.Warn("No! This will destroy you!", slog.Group(
		"user",
		"name", "bar",
		"drink": "vermouth",
	))

	slog.Error("I TOLD YOU NOT TO DO IT", slog.Group(
		"user",
		"name", "baz",
		"drink": "dizzy drinks",
	))

	slog.Debug("you can try to do anything lol idc", slog.Group(
		"user",
		"name", "qux",
		"drink": "all",
	))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

func NewHandler

func NewHandler(opts *slog.HandlerOptions) *Handler

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

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

Jump to

Keyboard shortcuts

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