Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

NO_COLOR

clog respects the NO_COLOR convention. When the NO_COLOR environment variable is set (any value, including empty), all colours and hyperlinks are disabled.

Colour Control

Colour behaviour is set per-Output via ColorMode:

// Package-level (recreates Default logger's Output)
clog.SetColorMode(clog.ColorAlways) // force colours (overrides NO_COLOR)
clog.SetColorMode(clog.ColorNever)  // disable all colours and hyperlinks
clog.SetColorMode(clog.ColorAuto)   // detect terminal capabilities (default)

// Per-logger via Output
logger := clog.New(clog.NewOutput(os.Stdout, clog.ColorAlways))

This is useful in tests to verify hyperlink output without mutating global state:

l := clog.New(clog.NewOutput(&buf, clog.ColorAlways))
l.Info().Line("file", "main.go", 42).Msg("Loaded")
// buf contains OSC 8 hyperlink escape sequences

ColorMode implements encoding.TextMarshaler and encoding.TextUnmarshaler, so it works directly with flag.TextVar and most flag libraries.

Piped stdout

When stdout is piped (non-TTY) but stderr is a terminal, styles created with lipgloss.NewStyle() normally lose their colours because lipgloss binds them to the global renderer (anchored to os.Stdout).

clog handles this transparently: all styles are automatically rebound to the logger’s output renderer via WithRenderer. This means colours work correctly on stderr even when stdout is redirected:

// Styles retain colours on stderr even when stdout is piped.
logger := clog.New(clog.Stderr(clog.ColorAuto))
logger.Info().Str("status", "ok").Msg("Ready")

For advanced use, Styles.WithRenderer and JSONStyles.WithRenderer are available to manually rebind styles to a specific *lipgloss.Renderer:

styles := clog.DefaultStyles()
styles.WithRenderer(output.Renderer())