Categorygithub.com/golang-cz/devslog
modulepackage
0.0.11
Repository: https://github.com/golang-cz/devslog.git
Documentation: pkg.go.dev

# README

🧻 devslog - Go slog.Handler for development

License: MIT 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

ParameterDescriptionDefaultValue
MaxSlicePrintSizeSpecifies the maximum number of elements to print for a slice.50uint
SortKeysDetermines if attributes should be sorted by keys.falsebool
TimeFormatTime format for timestamp."[15:04:05]"string
NewLineAfterLogAdd blank line after each logfalsebool
StringIndentationIndent \n in stringsfalsebool
DebugColorColor for Debug leveldevslog.Bluedevslog.Color (uint)
InfoColorColor for Info leveldevslog.Greendevslog.Color (uint)
WarnColorColor for Warn leveldevslog.Yellowdevslog.Color (uint)
ErrorColorColor for Error leveldevslog.Reddevslog.Color (uint)
MaxErrorStackTraceMax stack trace frames for errors0uint
StringerFormatterUse Stringer interface for formattingfalsebool
NoColorDisable coloringfalsebool

# Functions

No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Type aliases

No description provided by the author