A Go library for parsing and formatting stack traces with powerful customization options. This library makes debugging crashes and deliberate stack dumps more manageable by providing beautifully formatted stack traces with customizable styling.
go get github.com/kociumba/stackparse
The simplest way to use stackparse is to capture and parse a stack trace:
package main
import (
"os"
"runtime"
"github.com/kociumba/stackparse"
)
func main() {
// Create a buffer for the stack trace
buf := make([]byte, 1<<16)
// Capture the stack trace
runtime.Stack(buf, true)
// Parse and format the stack trace
parsed := stackparse.Parse(buf)
// modify the buffer in place with:
stackparse.ParseStatic(&buf)
// Write to stderr (or any other output)
os.Stderr.Write(parsed)
// if using stackparse.ParseStatic(), simply write from the buffer
os.Stderr.Write(buf)
}
Important
Right now stackparse does not parse the reason given when when calling panic()
, but this is planned for the next release.
stackparse provides several configuration options to customize the output:
Note
Default settings are: Colorize: true, Simple: true, Theme: stackparse.DefaultTheme()
Control whether the output includes ANSI color codes:
// Disable coloring the output
parsed := stackparse.Parse(buf, stackparse.WithColor(false))
Toggle between simple and detailed output formats (shortens both the function names and file paths):
// Disable simple mode for more detailed output, does not guarantee that the formatting will be correct in all cases
parsed := stackparse.Parse(buf, stackparse.WithSimple(false))
stackparse uses lipgloss for styling and provides powerful theming capabilities.
The default theme provides a color scheme based on the catppuccin theme.
// Stackparse uses the default theme without having to pass it in
parsed := stackparse.Parse(buf)
// You can get the default theme like this
defaultTheme := stackparse.DefaultTheme()
You can create custom themes by modifying the default theme or creating a new one from scratch:
// Create a custom theme based on the default
myTheme := stackparse.DefaultTheme()
// overwrite the default styles compleatly
myTheme.Goroutine = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#ff0000")) // Red goroutine labels
myTheme.Function = lipgloss.NewStyle().
Foreground(lipgloss.Color("#00ff00")). // Green function names
Italic(true)
// or build on top of the default theme
myTheme.Repeat = myTheme.Repeat.Faint(true).Blink(true)
// Apply the custom theme
parsed := stackparse.Parse(buf, stackparse.WithTheme(myTheme))
The Theme struct provides the following customizable components:
Goroutine
: Styling for goroutine headersFunction
: Styling for function namesArgs
: Styling for function argumentsFile
: Styling for file pathsLine
: Styling for line numbersCreatedBy
: Styling for "created by" sectionsRepeat
: Styling for repeated stack frames
You can control how styles are disabled when colors are turned off with:
myTheme := stackparse.DefaultTheme()
// Custom disable function
myTheme.SetDisableStylesFunc(func(t *stackparse.Theme) {
// Keep bold formatting but remove colors
t.Goroutine = t.Goroutine.UnsetForeground()
t.Function = t.Function.UnsetForeground()
t.Repeat = t.Repeat.UnsetBlink().UnsetFeint()
// Keep some styling intact
// everything else remains unchanged
})
parsed := stackparse.Parse(buf,
stackparse.WithTheme(myTheme),
stackparse.WithColor(false), // This will trigger the custom disable function
)
When using the default theme, your stack trace will be formatted like this:
Contributions are welcome! Please feel free to submit a Pull Request with anything: fixes, demos, improvements, etc.