humanslog

package module
v0.0.0-...-b7b6712 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 17 Imported by: 0

README

🤗 humanslog - Go slog.Handler for humans

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.

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: humanslog.Blue
	DebugColor Color

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

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

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

	// Max stack trace frames when unwrapping errors
	MaxErrorStackTrace uint

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

	// Disable coloring
	NoColor bool

	// Keep same color for whole source info, helpful when you want to open the line of code from terminal, but the ANSI coloring codes are in link itself
	SameSourceInfoColor bool
}

Jump to

Keyboard shortcuts

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