tint

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 11 Imported by: 0

README

tint: 🌈 slog.Handler that writes tinted logs

Go Reference Go Report Card



Package tint provides a slog.Handler that writes tinted logs. The output format is inspired by the zerolog.ConsoleWriter.

The output format can be customized using Options which is a drop-in replacement for slog.HandlerOptions.

go get github.com/lmittmann/tint

Note

slog is an experimental structured logging package, that will be added to the standard library in Go 1.21. See #56345 for tracking the progress.

Usage

// create a new logger
logger := slog.New(tint.NewHandler(os.Stderr))

// set global logger with custom options
slog.SetDefault(slog.New(tint.Options{
	Level:      slog.LevelDebug,
	TimeFormat: time.Kitchen,
}.NewHandler(os.Stderr)))
Customize

ReplaceAttr can be used to alter or drop attributes. If set, it is called on each non-group attribute before it is logged. See slog.HandlerOptions for details.

// create a new logger that doesn't write the time
logger := slog.New(tint.Options{
	ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
		if a.Key == slog.TimeKey && len(groups) == 0 {
			return slog.Attr{}
		}
		return a
	},
}.NewHandler(os.Stderr))
Windows

ANSI color support in the terminal on Windows can be enabled by using e.g. go-colorable.

logger := slog.New(tint.NewHandler(colorable.NewColorableStderr()))

Documentation

Overview

Package tint provides a slog.Handler that writes tinted logs. The output format is inspired by the zerolog.ConsoleWriter.

The output format can be customized using Options, which is a drop-in replacement for slog.HandlerOptions.

Example
package main

import (
	"errors"
	"os"
	"time"

	"github.com/fgimian/tint"
	"golang.org/x/exp/slog"
)

func main() {
	slog.SetDefault(slog.New(tint.Options{
		Level:      slog.LevelDebug,
		TimeFormat: time.Kitchen,
	}.NewHandler(os.Stderr)))

	slog.Info("Starting server", "addr", ":8080", "env", "production")
	slog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
	slog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
	slog.Error("DB connection lost", tint.Err(errors.New("connection reset")), "db", "myapp")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Err

func Err(err error) slog.Attr

Err returns a tinted slog.Attr.

func NewHandler

func NewHandler(w io.Writer) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options.

Types

type Options

type Options struct {
	// 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://pkgo.dev/golang.org/x/exp/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

	// 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.

func (Options) NewHandler

func (opts Options) NewHandler(w io.Writer) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w with the given options.

Jump to

Keyboard shortcuts

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