devslog

package module
v0.0.0-...-510d2da Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 15 Imported by: 0

README

🧻 devslog - Go slog.Handler for development

Go Report Card Go Reference

devslog is a zero dependency structured logging handler for Go's log/slog package with pretty and colorful output for developers.

Devslog output

image

Compared to

TextHandler image

JSONHandler image

Install

go get github.com/golang-cz/devslog@latest

Examples

Logger without options
logger := slog.New(devslog.NewHandler(os.Stdout, nil))

// optional: set global logger
slog.SetDefault(logger)
Logger with custom options
// new logger with options
opts := &devslog.Options{
	MaxSlicePrintSize: 4,
	SortKeys:          true,
	TimeFormat:        "[04:05]",
	NewLineAfterLog:   true,
	DebugColor:        devslog.Magenta,
	StringerFormatter: true,
}

logger := slog.New(devslog.NewHandler(os.Stdout, opts))

// optional: set global logger
slog.SetDefault(logger)
Logger with default slog options

Handler accepts default slog.HandlerOptions

// slog.HandlerOptions
slogOpts := &slog.HandlerOptions{
	AddSource:   true,
	Level:       slog.LevelDebug,
}

// new logger with options
opts := &devslog.Options{
	HandlerOptions:    slogOpts,
	MaxSlicePrintSize: 4,
	SortKeys:          true,
	NewLineAfterLog:   true,
	StringerFormatter: true,
}

logger := slog.New(devslog.NewHandler(os.Stdout, opts))

// optional: set global logger
slog.SetDefault(logger)
Example usage
slogOpts := &slog.HandlerOptions{
	AddSource: true,
	Level:     slog.LevelDebug,
}

var logger *slog.Logger
if production {
	logger = slog.New(slog.NewJSONHandler(os.Stdout, slogOpts))
} else {
	opts := &devslog.Options{
		HandlerOptions:    slogOpts,
		MaxSlicePrintSize: 10,
		SortKeys:          true,
		NewLineAfterLog:   true,
		StringerFormatter: true,
	}

	logger = slog.New(devslog.NewHandler(os.Stdout, opts))
}

// optional: set global logger
slog.SetDefault(logger)

Options

Parameter Description Default Value
MaxSlicePrintSize Specifies the maximum number of elements to print for a slice. 50 uint
SortKeys Determines if attributes should be sorted by keys. false bool
TimeFormat Time format for timestamp. "[15:04:05]" string
NewLineAfterLog Add blank line after each log false bool
StringIndentation Indent \n in strings false bool
DebugColor Color for Debug level devslog.Blue devslog.Color (uint)
InfoColor Color for Info level devslog.Green devslog.Color (uint)
WarnColor Color for Warn level devslog.Yellow devslog.Color (uint)
ErrorColor Color for Error level devslog.Red devslog.Color (uint)
MaxErrorStackTrace Max stack trace frames for errors 0 uint
StringerFormatter Use Stringer interface for formatting false bool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(out io.Writer, o *Options) *developHandler

Types

type Color

type Color uint
const (
	UnknownColor Color = iota
	Black
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

type Options

type Options struct {
	// You can use standard slog.HandlerOptions, that would be used in production
	*slog.HandlerOptions

	// Max number of printed elements in slice.
	MaxSlicePrintSize uint

	// If the attributes should be sorted by keys
	SortKeys bool

	// Time format for timestamp, default format is "[15:04:05]"
	TimeFormat string

	// Add blank line after each log
	NewLineAfterLog bool

	// Indent \n in strings
	StringIndentation bool

	// Set color for Debug level, default: devslog.Blue
	DebugColor Color

	// Set color for Info level, default: devslog.Green
	InfoColor Color

	// Set color for Warn level, default: devslog.Yellow
	WarnColor Color

	// Set color for Error level, default: devslog.Red
	ErrorColor Color

	// Max stack trace frames when unwrapping errors
	MaxErrorStackTrace uint

	// Use method String() for formatting value
	StringerFormatter bool
}

Jump to

Keyboard shortcuts

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