tint

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 9 Imported by: 894

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.

go get github.com/lmittmann/tint

Note

slog is an experimental structured logging package, that will be included in 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)))
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.

Example
package main

import (
	"errors"
	"os"
	"time"

	"github.com/lmittmann/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")
}

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 {
	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Level

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

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