Lightweight log/slog helper that:
- routes Info/Debug/Warn to a compact handler (no source info)
- routes Error+ to a handler with source locations
- highlights error attributes using
tint - optionally sets the logger as the global default, so other packages can just use
slog.*
go get github.com/vietddude/stylelogpackage main
import (
"errors"
"github.com/vietddude/stylelog"
)
func main() {
// Create and register the default logger using built-in options.
log := stylelog.InitDefault()
log.Debug("Debug message", "detail", "something")
log.Info("User login", "user", "alice", "ip", "127.0.0.1")
log.Warn("High memory usage", "usage", "85%")
err := errors.New("connection refused")
log.Error("Failed to load", "err", err)
}Any other package can now just use the global slog:
import (
"errors"
"log/slog"
)
func doSomething() {
testErr := errors.New("test error")
slog.Error("test error", "error", testErr)
}You can set the minimum log level by passing tint.Options:
package main
import (
"log/slog"
"github.com/lmittmann/tint"
"github.com/vietddude/stylelog"
)
func main() {
// Only log Warn and above
log := stylelog.InitDefault(&tint.Options{
Level: slog.LevelWarn,
})
log.Debug("This won't appear")
log.Info("This won't appear either")
log.Warn("This will appear", "key", "value")
log.Error("This will appear with source", "err", "something")
}You can pass your own tint.Options to New or InitDefault.
stylelog will:
- always disable
AddSourcefor low-level logs (Info/Debug/Warn) - always enable
AddSourcefor error logs - wrap your
ReplaceAttrso that"err"/"error"attributes stay highlighted in red
package main
import (
"log/slog"
"time"
"github.com/lmittmann/tint"
"github.com/vietddude/stylelog"
)
func main() {
opts := &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.RFC3339,
NoColor: false,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Example: rename "user_id" to "userID" in the output.
if a.Key == "user_id" {
a.Key = "userID"
}
return a
},
}
logger := stylelog.New(opts)
slog.SetDefault(logger)
slog.Info("Processing request", "user_id", 123)
slog.Error("Failed to process request", "user_id", 123, "err", "timeout")
}If you need more control, use New() directly:
package main
import (
"log/slog"
"github.com/lmittmann/tint"
"github.com/vietddude/stylelog"
)
func main() {
// Create logger without setting it as default
logger := stylelog.New(&tint.Options{
Level: slog.LevelInfo,
NoColor: true, // Disable colors for production logs
})
// Use it directly or set as default later
logger.Info("Application started")
// Set as default if needed
slog.SetDefault(logger)
}- Level-based routing: Low-level logs (Debug/Info/Warn) use a compact format, while errors include source file and line information
- Configurable: Set minimum log level, time format, colors, and custom attribute transformations
- Error highlighting:
erranderrorattributes are automatically highlighted in red for better visibility - Global default: Easy integration with existing code that uses
slog.*functions - Zero dependencies (except
tintand standard library)
MIT
