Skip to content

devslog - Go slog.Handler for development

License

Notifications You must be signed in to change notification settings

christiandins/devslog

 
 

Repository files navigation

🧻 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

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
NoColor Disable coloring false bool

About

devslog - Go slog.Handler for development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.5%
  • Makefile 0.5%