Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ThreeDotsLabs/humanslog

Open more actions menu
 
 

Repository files navigation

🤗 humanslog - Go slog.Handler for humans

License: MIT Go Report Card Go Reference

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

This is an updated version of github.com/golang-cz/devslog that keeps the colorful formatting and structure but writes most of the log output in a single line for better readability. Structs are formatted with proper indentation and alignment across multiple lines for clarity and readability. Multiline strings and JSON values are automatically formatted for optimal readability. I also adjusted color choices to be more suitable for the output format and closer to my personal taste.

Example output

Example output

Features

  • Single-line log format for most attributes
  • Multiline struct formatting with indentation and field alignment
  • Support for multiline strings
  • Inline JSON formatting with syntax highlighting
  • Colorful output with customizable colors
  • Zero dependencies
  • Stack trace support for errors
  • Logfmt-like output for inline attributes

Install

go get github.com/ThreeDotsLabs/humanslog@latest

Examples

Logger without options

logger := slog.New(humanslog.NewHandler(os.Stdout, nil))

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

Logger with custom options

// new logger with options
opts := &humanslog.Options{
	MaxSlicePrintSize: 4,
	SortKeys:          true,
	TimeFormat:        "[04:05]",
	NewLineAfterLog:   true,
	DebugColor:        humanslog.Magenta,
	StringerFormatter: true,
}

logger := slog.New(humanslog.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 := &humanslog.Options{
	HandlerOptions:    slogOpts,
	MaxSlicePrintSize: 4,
	SortKeys:          true,
	NewLineAfterLog:   true,
	StringerFormatter: true,
}

logger := slog.New(humanslog.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 := &humanslog.Options{
		HandlerOptions:    slogOpts,
		MaxSlicePrintSize: 10,
		SortKeys:          true,
		NewLineAfterLog:   true,
		StringerFormatter: true,
	}

	logger = slog.New(humanslog.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 humanslog.Blue humanslog.Color (uint)
InfoColor Color for Info level humanslog.Green humanslog.Color (uint)
WarnColor Color for Warn level humanslog.Yellow humanslog.Color (uint)
ErrorColor Color for Error level humanslog.Red humanslog.Color (uint)
MaxErrorStackTrace Max stack trace frames for errors 0 uint
StringerFormatter Use Stringer interface for formatting false bool
NoColor Disable coloring false bool
SameSourceInfoColor Keep same color for whole source info false bool

Credits

This project is based on github.com/golang-cz/devslog created by the golang-cz community. Special thanks to all the contributors of the original project for building an excellent foundation for structured logging in Go.

The original project provided the colorful output, configuration options, and overall architecture that made this single-line variant possible.

About

devslog - Go slog.Handler for development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.4%
  • Makefile 0.6%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.