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
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func Fatal(msg string, args ...any)
- func Handler() slog.Handler
- func Info(msg string, args ...any)
- func Init(opts ...Option)
- func Logger() *slog.Logger
- func SetCaller(enabled bool)
- func SetColorProfile(profile termenv.Profile)
- func SetLevel(level Level)
- func SetOutput(w io.Writer)
- func SetTimestamp(enabled bool)
- func Warn(msg string, args ...any)
- func With(args ...any) *slog.Logger
- func WithGroup(name string) *slog.Logger
- type Attr
- func Any(key string, value any) Attr
- func Bool(key string, value bool) Attr
- func Float64(key string, value float64) Attr
- func Group(key string, args ...any) Attr
- func Int(key string, value int) Attr
- func Int64(key string, value int64) Attr
- func String(key, value string) Attr
- func Uint64(key string, value uint64) Attr
- type Level
- type Option
Constants ¶
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 Fatal ¶
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 ¶
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 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 ¶
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 ¶
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 ¶
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.
Types ¶
type 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.
type 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 ¶
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 ¶
WithCaller toggles the file:line caller annotation on each log entry.
func WithTimestamp ¶
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.