colorized

package
v0.0.0-...-83d1dff Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 16 Imported by: 0

README

colorized

import "github.com/altessa-s/go-atlas/observability/slog/handler/colorized"

Package colorized provides a color-coded terminal slog.Handler for human-readable log output. Designed for CLI and development use.

Usage

h := colorized.NewHandler(os.Stderr,
    colorized.WithLevel(slog.LevelDebug),
    colorized.WithAddSource(),
    colorized.WithTimeFormat(time.Kitchen),
)
logger := slog.New(h)

Options

Option Description
WithLevel Minimum log level
WithAddSource Include source file and line
WithNoColor Disable ANSI colors
WithTimeFormat Custom time format string
WithReplaceAttr Attribute transformation function
WithAttributeColors Custom color map for attribute keys

Documentation

Overview

Package colorized provides a slog.Handler with color-coded terminal output. Use it for human-readable logs in CLI apps or development environments.

Example:

handler := colorized.NewHandler(os.Stdout, colorized.WithLevel(slog.LevelInfo))
logger := slog.New(handler)
logger.Info("started", "port", 8080)

Index

Constants

View Source
const (
	// DefaultBufferSize is the default initial buffer size (1KB).
	DefaultBufferSize = 1024
	// DefaultMaxBufferSize is the maximum buffer size kept in pool (16KB).
	DefaultMaxBufferSize = 16 << 10
)

Buffer pool configuration constants.

View Source
const (
	// DefaultTimeFormat is the default time format for log timestamps (RFC3339Nano).
	DefaultTimeFormat = time.RFC3339Nano
	// DefaultLogLevel is the default minimum log level (slog.LevelError).
	DefaultLogLevel = slog.LevelError
)
View Source
const CacheShards = 16

CacheShards is the number of shards used by the time cache.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(w io.Writer, opts ...Option) slog.Handler

NewHandler creates a colorized handler that writes to the provided io.Writer. Panics if w is nil. Uses default options if none provided.

Example:

h := colorized.NewHandler(os.Stdout)
logger := slog.New(h)

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implements slog.Handler with colorized terminal output. It uses lazy evaluation, lock-free caches, and buffer pooling for performance. Safe for concurrent use.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, r slog.Record) error

Handle formats and writes a log record with ANSI color codes.

func (*Handler) WithAttrs

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

WithAttrs returns a new Handler with the given attributes added.

func (*Handler) WithGroup

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

WithGroup returns a new Handler with the given group name prepended to keys.

type Option

type Option func(o *options)

Option is a functional option for configuring options.

func WithAddSource

func WithAddSource() Option

WithAddSource enables the addSource option.

func WithAttributeColors

func WithAttributeColors(v map[string][]int) Option

WithAttributeColors sets the attributeColors option.

func WithLevel

func WithLevel(v slog.Leveler) Option

WithLevel sets the level option.

func WithNoColor

func WithNoColor() Option

WithNoColor enables the noColor option.

func WithPrefixAttributeKey

func WithPrefixAttributeKey[T interface{ string | *string }](v T) Option

WithPrefixAttributeKey sets the prefixAttributeKey option.

func WithReplaceAttr

func WithReplaceAttr(replace func(groups []string, attr slog.Attr) slog.Attr) Option

WithReplaceAttr sets a function to transform or filter attributes before output.

func WithTimeFormat

func WithTimeFormat[T interface{ string | *string }](v T) Option

WithTimeFormat sets the timeFormat option.

Jump to

Keyboard shortcuts

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