console

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 12 Imported by: 11

README

console-slog

Go Reference license Build codecov Go Report Card

A handler for slog that prints colorized logs, similar to zerolog's console writer output without sacrificing performances.

Installation

go get github.com/phsym/console-slog@latest

Example

package main

import (
	"errors"
	"log/slog"
	"os"

	"github.com/phsym/console-slog"
)

func main() {
	logger := slog.New(
		console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug}),
	)
	slog.SetDefault(logger)
	slog.Info("Hello world!", "foo", "bar")
	slog.Debug("Debug message")
	slog.Warn("Warning message")
	slog.Error("Error message", "err", errors.New("the error"))

	logger = logger.With("foo", "bar").
		WithGroup("the-group").
		With("bar", "baz")

	logger.Info("group info", "attr", "value")
}

output

When setting console.HandlerOptions.AddSource to true:

console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug, AddSource: true})

output-with-source

Performances

See benchmark file for details.

The handler itself performs quite well compared to std-lib's handlers. It does no allocation:

goos: linux
goarch: amd64
pkg: github.com/phsym/console-slog
cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
BenchmarkHandlers/dummy-4               128931026            8.732 ns/op               0 B/op          0 allocs/op
BenchmarkHandlers/console-4               849837              1294 ns/op               0 B/op          0 allocs/op
BenchmarkHandlers/std-text-4              542583              2097 ns/op               4 B/op          2 allocs/op
BenchmarkHandlers/std-json-4              583784              1911 ns/op             120 B/op          3 allocs/op

However, the go 1.21.0 slog.Logger adds some overhead:

goos: linux
goarch: amd64
pkg: github.com/phsym/console-slog
cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
BenchmarkLoggers/dummy-4                 1239873             893.2 ns/op             128 B/op          1 allocs/op
BenchmarkLoggers/console-4                483354              2338 ns/op             128 B/op          1 allocs/op
BenchmarkLoggers/std-text-4               368828              3141 ns/op             132 B/op          3 allocs/op
BenchmarkLoggers/std-json-4               393322              2909 ns/op             248 B/op          4 allocs/op

Documentation

Index

Constants

View Source
const (
	Reset = iota
	Bold
	Faint
	Italic
	Underline
	CrossedOut = 9
)
View Source
const (
	Black = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	Gray
)
View Source
const (
	BrightBlack = iota + 90
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	White
)

Variables

View Source
var ResetMod = ToANSICode(Reset)

Functions

This section is empty.

Types

type ANSIMod added in v0.3.0

type ANSIMod string

func ToANSICode added in v0.3.0

func ToANSICode(modes ...int) ANSIMod

func (ANSIMod) String added in v0.3.0

func (c ANSIMod) String() string

type Handler added in v0.1.0

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

func NewHandler

func NewHandler(out io.Writer, opts *HandlerOptions) *Handler

NewHandler creates a Handler that writes to w, using the given options. If opts is nil, the default options are used.

func (*Handler) Enabled added in v0.1.0

func (h *Handler) Enabled(_ context.Context, l slog.Level) bool

Enabled implements slog.Handler.

func (*Handler) Handle added in v0.1.0

func (h *Handler) Handle(_ context.Context, rec slog.Record) error

Handle implements slog.Handler.

func (*Handler) WithAttrs added in v0.1.0

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

WithAttrs implements slog.Handler.

func (*Handler) WithGroup added in v0.1.0

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

WithGroup implements slog.Handler.

type HandlerOptions

type HandlerOptions struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes LevelInfo.
	// The handler calls Level.Level for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level slog.Leveler

	// Disable colorized output
	NoColor bool

	// TimeFormat is the format used for time.DateTime
	TimeFormat string

	// Theme defines the colorized output using ANSI escape sequences
	Theme Theme
}

HandlerOptions are options for a ConsoleHandler. A zero HandlerOptions consists entirely of default values.

type Theme added in v0.3.0

type Theme interface {
	Name() string
	Timestamp() ANSIMod
	Source() ANSIMod

	Message() ANSIMod
	MessageDebug() ANSIMod
	AttrKey() ANSIMod
	AttrValue() ANSIMod
	AttrValueError() ANSIMod
	LevelError() ANSIMod
	LevelWarn() ANSIMod
	LevelInfo() ANSIMod
	LevelDebug() ANSIMod
	Level(level slog.Level) ANSIMod
}

func NewBrightTheme added in v0.3.0

func NewBrightTheme() Theme

func NewDefaultTheme added in v0.3.0

func NewDefaultTheme() Theme

type ThemeDef added in v0.3.0

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

func (ThemeDef) AttrKey added in v0.3.0

func (t ThemeDef) AttrKey() ANSIMod

func (ThemeDef) AttrValue added in v0.3.0

func (t ThemeDef) AttrValue() ANSIMod

func (ThemeDef) AttrValueError added in v0.3.0

func (t ThemeDef) AttrValueError() ANSIMod

func (ThemeDef) Level added in v0.3.0

func (t ThemeDef) Level(level slog.Level) ANSIMod

func (ThemeDef) LevelDebug added in v0.3.0

func (t ThemeDef) LevelDebug() ANSIMod

func (ThemeDef) LevelError added in v0.3.0

func (t ThemeDef) LevelError() ANSIMod

func (ThemeDef) LevelInfo added in v0.3.0

func (t ThemeDef) LevelInfo() ANSIMod

func (ThemeDef) LevelWarn added in v0.3.0

func (t ThemeDef) LevelWarn() ANSIMod

func (ThemeDef) Message added in v0.3.0

func (t ThemeDef) Message() ANSIMod

func (ThemeDef) MessageDebug added in v0.3.0

func (t ThemeDef) MessageDebug() ANSIMod

func (ThemeDef) Name added in v0.3.0

func (t ThemeDef) Name() string

func (ThemeDef) Source added in v0.3.0

func (t ThemeDef) Source() ANSIMod

func (ThemeDef) Timestamp added in v0.3.0

func (t ThemeDef) Timestamp() ANSIMod

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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