Skip to content

Marlliton/slogpretty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slogpretty ✨

A customizable and colorful handler for Go's slog designed for structured, readable, and beautiful terminal output.

demo banner

Features

  • 🌈 Colored log levels (DEBUG, INFO, WARN, ERROR)
  • 📝 Structured attributes with clean formatting
  • 📂 Smart source tracking (file:line)
  • 🪄 Multiline mode for complex data
  • ⏱️ Custom timestamp formatting

Installation

go get github.com/Marlliton/slogpretty

Basic Usage

Set up slogpretty as the default handler:

package main

import (
	"log/slog"
	"os"

	"github.com/Marlliton/slogpretty"
)

func main() {
	// Minimal setup
	handler := slogpretty.New(os.Stdout, nil)
	slog.SetDefault(slog.New(handler))

	slog.Info("Server started", "port", 8080)
}

minimal setup

Advanced Configuration

You can configure the handler using the Options struct:

handler := slogpretty.New(os.Stdout, &slogpretty.Options{
	Level:      slog.LevelDebug,
	AddSource:  true,                            // Show file location
	Colorful:   true,                            // Enable colors. Default is true
	Multiline:  true,                            // Pretty print for complex data
	TimeFormat: slogpretty.DefaultTimeFormat,    // Custom format (e.g., time.Kitchen)
})

advanced configuration

Features

  1. Colored Levels

    Each log level has a distinct color.

  2. Structured Attributes

    Clean formatting for attributes.

	slog.Debug("Debugging data")
	slog.Info("Informational message")
	slog.Warn("Potential issue detected")
	slog.Error("Operation failed")
	slog.Info("User logged in", "user_id", 1234, "email", "[email protected]", "active", true)

structured attributes

  1. Complex Data in Multiple Lines
	slog.Info("Event with group and subgroups",
		"user", "bob",
		slog.Group("details",
			slog.Int("port", 8080),
			slog.String("status", "inactive"),
			slog.Group("metrics",
				slog.Float64("cpu", 72.5),
				slog.Float64("memory", 65.3),
			),
			slog.Group("location",
				slog.String("country", "Brazil"),
				slog.String("region", "SP"),
				slog.Group("coordinates",
					slog.Float64("lat", -23.5505),
					slog.Float64("lon", -46.6333),
				),
			),
		),
		"session", "0x93AF21",
		"authenticated", false,
	)

multiline

💡 Best Practices

🛠 Development: Enable as Many Features as You Like

In development environments, it's recommended to enable as many features as possible to improve log readability, debugging, and traceability:

&pretty.Options{
    Level:     slog.LevelDebug,
    AddSource: true,
    Colorful:  true,
    Multiline: true,
}

These options provide:

  • Logs from the debug level (LevelDebug)
  • Source tracking (AddSource)
  • Colored output for better terminal visibility (Colorful)
  • Structured multiline formatting (Multiline)

🚀 Production: Use slog's Native JSONHandler

For production, it's best to use slog.NewJSONHandler, which generates logs in JSON format:

JSONhandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
	Level: slog.LevelInfo,
})
slog.SetDefault(slog.New(JSONhandler))

slog.Info("Event with groups and subgroups",
	"user", "bob",
	slog.Group("details",
		slog.Int("port", 8080),
		slog.String("status", "inactive"),
		slog.Group("metrics",
			slog.Float64("cpu", 72.5),
			slog.Float64("memory", 65.3),
		),
		slog.Group("location",
			slog.String("country", "Brazil"),
			slog.String("region", "SP"),
			slog.Group("coordinates",
				slog.Float64("lat", -23.5505),
				slog.Float64("lon", -46.6333),
			),
		),
	),
	"session", "0x93AF21",
	"authenticated", false,
)

✅ Why Use JSON in Production?

  • Compatible with observability tools (e.g., Datadog, Loki, Grafana)
  • Widely accepted format — simplifies analysis, searching, etc.
  • Standardized structure — ideal for distributed systems and centralized logging
  • Efficient — avoids overhead from unnecessary visual formatting

About

Custom slog handler with colorful and structured log output for Go.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages