devslog

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 11 Imported by: 35

README

🧻 devslog - slog.Handler for developing code

Go Report Card Go Reference

devslog is zero dependency custom logging handler for Go's standard log/slog package that provides structured logging with colorful and indented structure for developing.

Develop with this output

image

Instead of these outputs

TextHandler image JSONHandler image

Install

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

Examples

Logger without options
w := os.Stdout

logger := slog.New(devslog.NewHandler(w, nil))

// set global logger
slog.SetDefault(logger)
Logger with custom options
w := os.Stdout

// new logger with options
opts := &devslog.Options{
	MaxSlicePrintSize: 4,
	SortKeys:          true,
	TimeFormat:        "[06:05]"
}

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

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

Handler accept default slog.HandlerOptions

w := os.Stdout

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

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

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

// optional: set global logger
slog.SetDefault(logger)
Example of initialization with production
production := false

w := os.Stdout

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

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

	logger = slog.New(devslog.NewHandler(w, 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:06:05]" string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

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

Types

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:06:05]"
	TimeFormat string
}

Jump to

Keyboard shortcuts

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