log

package
v0.0.3-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package log is a slog-first logger backed by charmbracelet/log's slog.Handler implementation.

The public API is a small wrapper over log/slog: Debug/Info/Warn/Error (and hex-specific Fatal) delegate to slog.Default, which is installed by Init to point at a charmbracelet/log handler. Consumers who need attribute grouping, per-request loggers, or a distinct handler use Logger()/Handler() to reach the underlying slog types directly.

Attribute helpers (log.String, log.Int, log.Group, ...) are re-exported from log/slog for convenience so callers do not have to import both packages side-by-side.

Init runs once from main; downstream providers use SetLevel to adjust verbosity at runtime.

Index

Constants

View Source
const (
	DebugLevel = slog.LevelDebug // -4
	InfoLevel  = slog.LevelInfo  //  0
	WarnLevel  = slog.LevelWarn  //  4
	ErrorLevel = slog.LevelError //  8
	FatalLevel = Level(12)       // matches charmbracelet/log.FatalLevel
)

Level constants. Numeric values match slog.Level (which in turn matches charmbracelet/log.Level 1:1). FatalLevel is a hex extension that mirrors charmbracelet/log.FatalLevel — slog has no Fatal so hex.log.Fatal below routes through the charm handler directly.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs at DebugLevel via slog.Default.

func Error

func Error(msg string, args ...any)

Error logs at ErrorLevel via slog.Default.

func Fatal

func Fatal(msg string, args ...any)

Fatal logs at FatalLevel via the charmbracelet handler (slog has no Fatal) and terminates the process with status 1. Do not call from library code; reserve it for main.

func Handler

func Handler() slog.Handler

Handler returns slog.Default().Handler(). After Init installs hex's charmbracelet handler this is the same handler; if a consumer swaps slog.Default externally (custom JSON handler, OTel exporter, etc.) this follows them — the whole point of routing through slog is that downstream code can redirect logs without knowing about hex/log.

func Info

func Info(msg string, args ...any)

Info logs at InfoLevel via slog.Default.

func Init

func Init(opts ...Option)

Init configures the global slog handler and installs it via slog.SetDefault. Idempotent — the last call wins.

Defaults: level FatalLevel (silent until the app opts in), caller and timestamp off, hex's colour palette applied. A typical main calls Init exactly once, before hex.App.Bootstrap.

func Logger

func Logger() *slog.Logger

Logger returns slog.Default(). Handy for callers wanting to hold onto a logger reference, use With/WithGroup, or pass to libraries that accept *slog.Logger.

func SetCaller

func SetCaller(enabled bool)

SetCaller toggles the file:line caller annotation on each log entry.

func SetColorProfile

func SetColorProfile(profile termenv.Profile)

SetColorProfile locks the charm handler's ANSI color profile. Overrides the TTY-detection heuristic that would otherwise strip colors when writing to a non-terminal (buffer, pipe, file).

The REPL uses this so log output rendered through Bubble Tea's scrollback keeps its colors even though the underlying writer (a strings.Builder) looks like a non-TTY to termenv.

termenv.Ascii, ANSI, ANSI256, TrueColor for gradually richer palettes. Pass termenv.EnvColorProfile() to honour $TERM / $COLORTERM.

func SetLevel

func SetLevel(level Level)

SetLevel updates the minimum log level of the global handler. Lazily initialises a default handler if Init has not run.

func SetOutput

func SetOutput(w io.Writer)

SetOutput redirects hex's charmbracelet handler to write to w. Applies globally — subsequent log calls from anywhere in the process go to w until the next SetOutput call.

The REPL uses this per-eval to capture log output into scrollback instead of writing to os.Stderr behind Bubble Tea's back. General consumers can point logs at a file, buffer, or io.MultiWriter.

No-op if a consumer has swapped slog.Default externally to a non-charm handler; redirect via that handler's own API instead.

func SetTimestamp

func SetTimestamp(enabled bool)

SetTimestamp toggles timestamps on each log entry.

func Warn

func Warn(msg string, args ...any)

Warn logs at WarnLevel via slog.Default.

func With

func With(args ...any) *slog.Logger

With returns a *slog.Logger with the given attributes attached to every subsequent record. Shorthand for slog.Default().With(args...).

func WithGroup

func WithGroup(name string) *slog.Logger

WithGroup returns a *slog.Logger that starts a new attribute group. Every subsequent attribute becomes a nested field of that group in structured backends. Shorthand for slog.Default().WithGroup(name).

Types

type Attr

type Attr = slog.Attr

Attr is Go's standard slog attribute type. Aliased so callers writing log.With(log.String("k","v")) do not need to import log/slog directly.

func Any

func Any(key string, value any) Attr

Any returns an Attr for any value; the handler decides how to render.

func Bool

func Bool(key string, value bool) Attr

Bool returns an Attr for a bool value.

func Float64

func Float64(key string, value float64) Attr

Float64 returns an Attr for a float64 value.

func Group

func Group(key string, args ...any) Attr

Group returns an Attr containing a nested group of attributes. Handlers render this as a sub-object in structured output and as prefixed keys in text output.

func Int

func Int(key string, value int) Attr

Int returns an Attr for an int value.

func Int64

func Int64(key string, value int64) Attr

Int64 returns an Attr for an int64 value.

func String

func String(key, value string) Attr

String returns an Attr for a string value.

func Uint64

func Uint64(key string, value uint64) Attr

Uint64 returns an Attr for a uint64 value.

type Level

type Level = slog.Level

Level is Go's standard log/slog level type. Aliased so consumers can pass hex.log.Level anywhere slog.Level is expected.

func GetLevel

func GetLevel() Level

GetLevel returns the current minimum log level. Lazily initialises a default handler if Init has not run.

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel converts a string like "info" or "warn" into a Level. Wraps charmbracelet/log's parser so callers do not need to import it.

type Option

type Option func(*options)

Option configures the logger. See Init.

func WithCaller

func WithCaller(enabled bool) Option

WithCaller toggles the file:line caller annotation on each log entry.

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the minimum level of messages that are emitted.

func WithTimestamp

func WithTimestamp(enabled bool) Option

WithTimestamp toggles timestamps on each log entry.

func WithoutStyles

func WithoutStyles() Option

WithoutStyles disables hex's default colour palette. Use when the consumer wants to install its own styles or produce plain output.

Directories

Path Synopsis
Package lua exposes hex/log to Lua scripts as the "log" module.
Package lua exposes hex/log to Lua scripts as the "log" module.
Package provider is the default hex/log service provider.
Package provider is the default hex/log service provider.

Jump to

Keyboard shortcuts

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