loggers

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

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

Go to latest
Published: Jul 20, 2024 License: MPL-2.0 Imports: 13 Imported by: 2

README

Loggers

Loggers is a collection of log/slog handlers that pretty print logs, similar to Zerolog's ConsoleWriter, but without sacrificing any performance.

Example

package main

import (
	"log/slog"
	"os"
	"strconv"

	"go.elara.ws/loggers"
)

const input = "hello"

func main() {
	log := slog.New(loggers.NewPretty(os.Stdout, loggers.Options{
		Level: slog.LevelDebug,
		ShowCaller: true,
	}))

	i, err := strconv.Atoi(input)
	if err != nil {
		log.Error(
			"Couldn't convert to integer",
			slog.String("input", input),
			slog.Any("error", err),
		)
		return
	}

	log.Info(
		"Converted to integer!",
		slog.Int("output", i),
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI

type CLI struct {

	// Colorize indicates whether colors will be used
	// in log output.
	Colorize bool

	// Out is where log output will be written.
	Out io.Writer

	Options
	// contains filtered or unexported fields
}

CLI is an slog handler for command-line tools where users will view and read the logs throughout the application's execution.

func NewCLI

func NewCLI(wr io.Writer, opts Options) *CLI

NewPretty creates and returns a new CLI handler.

func (*CLI) Enabled

func (c *CLI) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*CLI) Handle

func (c *CLI) Handle(_ context.Context, rec slog.Record) error

Handle formats the given log/slog.Record as a human-readable string on a single line.

func (*CLI) WithAttrs

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

WithAttrs returns a new CLI handler whose attributes consists of c's attributes followed by attrs.

func (*CLI) WithGroup

func (c *CLI) WithGroup(group string) slog.Handler

WithGroup returns a new CLI handler with the given group name.

type Options

type Options struct {
	// TimeFormat represents the format of the timestamp
	// provided by the [Pretty] logger.
	TimeFormat string

	// Level is the log level above which the handler will
	// handle records.
	Level slog.Level

	// ShowCaller indicates whether the caller that created the
	// record should be provided in the output
	ShowCaller bool

	// ForceColors prevents checking whether a handler outputs
	// to a valid tty and always enables colors if set to true.
	ForceColors bool
}

Options represents common options for slog handlers in this package.

type Pretty

type Pretty struct {

	// Colorize indicates whether colors will be used
	// in log output.
	Colorize bool

	// Out is where log output will be written.
	Out io.Writer

	Options
	// contains filtered or unexported fields
}

Pretty is an slog handler with a human-readable output

func NewPretty

func NewPretty(wr io.Writer, opts Options) *Pretty

NewPretty creates and returns a new Pretty handler. If opts doesn't specify a time format, time.Kitchen will be used.

func (*Pretty) Enabled

func (p *Pretty) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*Pretty) Handle

func (p *Pretty) Handle(_ context.Context, rec slog.Record) error

Handle formats the given log/slog.Record as a human-readable string on a single line.

func (*Pretty) WithAttrs

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

WithAttrs returns a new Pretty handler whose attributes consists of p's attributes followed by attrs.

func (*Pretty) WithGroup

func (p *Pretty) WithGroup(name string) slog.Handler

WithGroup returns a new Pretty handler with the given group name.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳