stylelog

package module
v1.0.0 Latest Latest
Warning

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

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

README

stylelog

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.*

stylelog demo

Install

go get github.com/vietddude/stylelog

Usage

Basic Usage
package 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)
}
Customizing Log Level

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")
}
Customizing tint.Options

You can pass your own tint.Options to New or InitDefault.
stylelog will:

  • always disable AddSource for low-level logs (Info/Debug/Warn)
  • always enable AddSource for error logs
  • wrap your ReplaceAttr so 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")
}
Advanced Usage

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)
}

Features

  • 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: err and error attributes are automatically highlighted in red for better visibility
  • Global default: Easy integration with existing code that uses slog.* functions
  • Zero dependencies (except tint and standard library)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitDefault

func InitDefault(opts ...*tint.Options) *slog.Logger

InitDefault creates the logger from New, sets it as slog's default, and returns it for direct use.

func New

func New(opts ...*tint.Options) *slog.Logger

New returns a slog.Logger that: - uses a tint handler without source for Info/Debug/Warn - uses a tint handler with source (and red-colored "err"/"error" fields) for Error+ - optionally applies the provided tint.Options (if any) to both handlers

If opts is provided, the first element is used as the base options for both handlers. The following fields are still controlled by stylelog:

  • low-level handler: AddSource is forced to false
  • error handler: AddSource is forced to true and its ReplaceAttr is wrapped to also color "err"/"error" attributes red.

Types

type LevelBasedHandler

type LevelBasedHandler struct {
	LowLevelHandler slog.Handler
	ErrorHandler    slog.Handler
}

LevelBasedHandler routes log records to different handlers based on level: - Info, Debug, Warn -> LowLevelHandler (no source, lighter output) - Error and above -> ErrorHandler (with source, highlighted errors)

func (*LevelBasedHandler) Enabled

func (h *LevelBasedHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*LevelBasedHandler) Handle

func (h *LevelBasedHandler) Handle(ctx context.Context, r slog.Record) error

func (*LevelBasedHandler) WithAttrs

func (h *LevelBasedHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LevelBasedHandler) WithGroup

func (h *LevelBasedHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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