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

Printer

Output styled data directly without a log level, key, or message:

clog.Print().RawJSON([]byte(`{"status":"ok","count":42,"active":true}`))
// {
//   "status": "ok",
//   "count": 42,
//   "active": true
// }

The Printer writes directly to the logger’s output, bypassing any custom Handler. It uses the logger’s style configurations for token colors.

JSON

JSON marshals any Go value; RawJSON accepts pre-serialized bytes:

clog.Print().JSON(userStruct)
clog.Print().RawJSON(responseBody)

The default mode is JSONPretty, which pretty-prints with indentation. Set the global default with SetJSONPrintMode, or override per-call with Mode:

// Global default: flatten to single line
clog.SetJSONPrintMode(clog.JSONFlat)

// Per-call override
clog.Print().Mode(clog.JSONPretty).RawJSON(data)
clog.Print().Mode(clog.JSONFlat).RawJSON(data)
ModeDescription
JSONPrettyPretty-print with normalized indentation (default)
JSONFlatFlatten to a single line (matches inline log fields)
JSONPreserveKeep original whitespace, only add syntax highlighting

Styling

Printer JSON inherits token colors (keys, strings, numbers, etc.) from the logger’s JSON styles. Field-specific rendering modes (JSONModeHuman, JSONModeFlat) are not applied – the Printer always uses standard JSON rendering.

custom := style.DefaultJSON()
custom.Key = new(lipgloss.NewStyle().Foreground(lipgloss.Color("#50fa7b")))
clog.SetStyles(&style.Config{JSON: custom})

YAML

YAML marshals any Go value; RawYAML accepts pre-serialized bytes:

clog.Print().YAML(configStruct)
clog.Print().RawYAML(responseBody)

See YAML for styling options.

TOML

TOML marshals any Go value; RawTOML accepts pre-serialized bytes:

clog.Print().TOML(configStruct)
clog.Print().RawTOML(configBytes)

See TOML for styling options.

HCL

RawHCL accepts pre-serialized HCL bytes (there is no marshal method since HCL has no standard Go marshal API):

clog.Print().RawHCL(terraformConfig)

See HCL for styling options.

Themes

Printer styles default to terminal-aware light/dark selection: on the first colored write the logger detects the background of its own output and picks a matching theme (dark mode preserves the original Dracula-based colors). The terminal is queried at most once, and never for non-terminal or color-disabled outputs, which use the dark theme. Switch all four format styles at once with SetTheme, which takes a light/dark pair:

clog.SetTheme(theme.MustPair(theme.CatppuccinLatte(), theme.Dracula()))

For a fixed theme regardless of the terminal background, wrap it with theme.Single:

clog.SetTheme(theme.Single(theme.Monokai()))

Per-token overrides still work after setting a theme:

clog.SetTheme(theme.Single(theme.Monokai()))
s := clog.DefaultStyles()
s.JSON.Key = new(lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")))
clog.SetStyles(s)

To build styles from a theme directly:

custom := style.NewJSON(theme.Monokai())

Environment variables

The default logger reads its theme from the environment (highest precedence first):

  1. CLOG_THEME - an explicit theme, applied on both backgrounds (via theme.Single):

    CLOG_THEME=monokai
    
  2. CLOG_THEME_LIGHT and CLOG_THEME_DARK - a light/dark pair; the entry matching the terminal background is selected on the first write:

    CLOG_THEME_LIGHT=catppuccin-latte
    CLOG_THEME_DARK=dracula
    

CLOG_THEME takes precedence over the light/dark pair when both are set. With a custom prefix (see SetEnvPrefix), <PREFIX>_THEME* is checked first, then the CLOG_* fallback.

The same pair can also be loaded programmatically:

themes, err := theme.PairFromEnv()
if err != nil {
    return err
}
clog.SetTheme(themes)

Available themes:

  • theme.Dark()
  • theme.Light()
  • theme.CatppuccinFrappe()
  • theme.CatppuccinLatte()
  • theme.CatppuccinMacchiato()
  • theme.CatppuccinMocha()
  • theme.Dracula()
  • theme.Monokai()

Indentation

The default indent is two spaces. SetPrintIndent sets the baseline for all formats; SetJSONIndent and SetYAMLIndent override it for a specific format:

clog.SetPrintIndent("\t")       // tabs for both JSON and YAML
clog.SetJSONIndent("    ")      // 4 spaces for JSON only
clog.SetYAMLIndent("  ")        // 2 spaces for YAML only

YAML sequences are indented under their parent key by default. Disable with SetYAMLIndentSequence(false).

Sub-loggers

Each logger has its own Printer, so sub-loggers with different styles produce different output:

logger := clog.NewWriter(os.Stdout)
logger.SetStyles(&style.Config{
    JSON: style.DefaultJSON().WithSpacing(style.JSONSpacingAll),
})

logger.Print().RawJSON(data)