slogcolor

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: MIT Imports: 12 Imported by: 2

README

🌈 slogcolor

Go Reference Go Report Card

screenshot

slogcolor is a color handler for log/slog. It's output is inspired by XMRig and zerolog.

Basic Usage

package main

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

    "github.com/MatusOllah/slogcolor"
)

func main() {
    slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions)))

    slog.Info("Initializing")
    slog.Debug("Init done", "duration", 500*time.Millisecond)
    slog.Warn("Slow request!", "method", "GET", "path", "/api/users", "duration", 750*time.Millisecond)
    slog.Error("DB connection lost!", "err", errors.New("connection reset"), "db", "horalky")
}

Documentation

Overview

slogcolor implements a color handler for log/slog.

Example
package main

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

	"github.com/MatusOllah/slogcolor"
)

func main() {
	opts := slogcolor.DefaultOptions
	opts.Level = slog.LevelDebug
	slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))

	slog.Info("Initializing")
	slog.Debug("Init done", "duration", 500*time.Millisecond)
	slog.Warn("Slow request!", "method", "GET", "path", "/api/users", "duration", 750*time.Millisecond)
	slog.Error("DB connection lost!", "err", errors.New("connection reset"), "db", "horalky")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Prefix added in v1.1.0

func Prefix(prefix string, msg string) string

Types

type Handler

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

func NewHandler

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

NewHandler creates a new Handler.

func (*Handler) Enabled

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

Enabled implements slog.Handler.Enabled .

func (*Handler) Handle

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

Handle implements slog.Handler.Handle .

func (*Handler) WithAttrs

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

WithAttrs implements slog.Handler.WithAttrs .

func (*Handler) WithGroup

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

WithGroup implements slog.Handler.WithGroup .

type Options

type Options struct {
	// Level reports the minimum level to log.
	// Levels with lower levels are discarded.
	// If nil, the Handler uses [slog.LevelInfo].
	Level slog.Leveler

	// The time format.
	TimeFormat string

	// SrcFileMode is the source file mode.
	SrcFileMode SourceFileMode

	// SrcFileLength to show fixed length filename to line up the log output, default 0 shows complete filename
	SrcFileLength int

	// MsgPrefix to show prefix before message, default: white colored "| "
	MsgPrefix string

	// MsgColor is the color of the message, default to empty
	MsgColor *color.Color

	// MsgLength to show fixed length message to line up the log output, default 0 shows complete message
	MsgLength int
}
var DefaultOptions *Options = &Options{
	Level:         slog.LevelInfo,
	TimeFormat:    time.DateTime,
	SrcFileMode:   ShortFile,
	SrcFileLength: 0,
	MsgPrefix:     color.HiWhiteString("| "),
	MsgLength:     0,
	MsgColor:      color.New(),
}

type SourceFileMode

type SourceFileMode int
const (
	// Nop does nothing.
	Nop SourceFileMode = iota

	// ShortFile produces only the filename (for example main.go:69).
	ShortFile

	// LongFile produces the full file path (for example /home/frajer/go/src/myapp/main.go:69).
	LongFile
)

Jump to

Keyboard shortcuts

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