🤗 humanslog - Go slog.Handler for humans
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.
- 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
go get github.com/ThreeDotsLabs/humanslog@latest
logger := slog.New(humanslog.NewHandler(os.Stdout, nil))
// optional: set global logger
slog.SetDefault(logger)// 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)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)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)| 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 |
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.
