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

Styles

Customise the visual appearance using lipgloss styles:

Styled output

clog.SetStyles(&style.Config{
  // Customise level colors
  Levels: style.LevelMap{
    clog.LevelError: new(
      lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")), // bright red
    ),
  },
  // Customise field key appearance
  KeyDefault: new(
    lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12")), // bright blue
  ),
})

SetStyles merges non-zero fields into the existing configuration - only the fields you set are changed, all others keep their current values.

Value Coloring

Values are styled with a three-tier priority system:

  1. Key styles - style all values of a specific field key
  2. Value styles - style values matching a typed key (bool true != string "true")
  3. Type styles - style values by their Go type
clog.SetStyles(&style.Config{
  // 1. Key styles: all values of the "status" field are green
  Keys: style.Map{
    "status": new(lipgloss.NewStyle().Foreground(lipgloss.Color("2"))),
  },
  // 2. Value styles: typed key matches (bool `true` != string "true")
  Values: style.ValueMap{
    "PASS": new(lipgloss.NewStyle().Foreground(lipgloss.Color("2"))),
    "FAIL": new(lipgloss.NewStyle().Foreground(lipgloss.Color("1"))),
  },
  // 3. Type styles
  FieldString: new(lipgloss.NewStyle().Foreground(lipgloss.Color("15"))),
  FieldNumber: new(lipgloss.NewStyle().Foreground(lipgloss.Color("5"))),
  FieldError:  new(lipgloss.NewStyle().Foreground(lipgloss.Color("1"))),
})

Styles Reference

FieldTypeAliasDefault
DurationGradient[]style.ColorStopgreen → yellow → red
DurationGradientModestyle.GradientModestyle.GradientFade
DurationThresholdsmap[string][]ThresholdThresholdMap{}
DurationUnitsmap[string]StyleStyleMap{}
ElapsedGradient[]style.ColorStopgreen → yellow → red
ElapsedGradientModestyle.GradientModestyle.GradientFade
FieldDurationNumberStylemagenta
FieldDurationUnitStylemagenta faint
FieldElapsedNumberStylenil (→ DurationNumber)
FieldElapsedUnitStylenil (→ DurationUnit)
FieldErrorStylered
FieldNumberStylemagenta
FieldPercentStylenil
FieldQuantityNumberStylemagenta
FieldQuantityUnitStylemagenta faint
FieldStringStylewhite
FieldTimeStylemagenta
KeyDefaultStyleblue
Keysmap[string]StyleStyleMap{}
Levelsmap[Level]StyleLevelStyleMapper-level bold colors
Messagesmap[Level]StyleLevelStyleMapstyle.DefaultMessages()
PercentGradient[]style.ColorStopred → yellow → green
QuantityThresholdsmap[string][]ThresholdThresholdMap{}
QuantityUnitsmap[string]StyleStyleMap{}
SeparatorStylefaint
Symbolsmap[Level]StyleLevelStyleMap{}
TimestampStylefaint
Valuesmap[any]StyleValueStyleMapstyle.DefaultValues()

Syntax Highlighting

Per-token styles for the Printer. Each has a Default*() constructor with Dracula-inspired colors. Set to nil to disable highlighting for that format.

FieldTypeDefault
HCL*style.HCLstyle.DefaultHCL()
JSON*style.JSONstyle.DefaultJSON()
TOML*style.TOMLstyle.DefaultTOML()
YAML*style.YAMLstyle.DefaultYAML()

See Printer for per-format token style tables.

Field Descriptions

FieldDescription
DurationGradientGradient color stops for Duration fields; active when SetDurationGradientMax > 0
DurationGradientModeGradient transition mode: GradientFade (smooth) or GradientStep (discrete)
DurationThresholdsDuration unit -> magnitude-based style thresholds
DurationUnitsDuration unit string -> style override
ElapsedGradientGradient color stops for Elapsed fields; active when SetElapsedGradientMax > 0
ElapsedGradientModeGradient transition mode: GradientFade (smooth) or GradientStep (discrete)
FieldDurationNumberStyle for numeric segments of duration values (e.g. “1” in “1m30s”), nil to disable
FieldDurationUnitStyle for unit segments of duration values (e.g. “m” in “1m30s”), nil to disable
FieldElapsedNumberStyle for numeric segments of elapsed-time values; nil falls back to FieldDurationNumber
FieldElapsedUnitStyle for unit segments of elapsed-time values; nil falls back to FieldDurationUnit
FieldErrorStyle for error field values, nil to disable
FieldNumberStyle for int/float field values, nil to disable
FieldPercentBase style for Percent fields (foreground overridden by gradient), nil to disable
FieldQuantityNumberStyle for numeric part of quantity values (e.g. “5” in “5km”), nil to disable
FieldQuantityUnitStyle for unit part of quantity values (e.g. “km” in “5km”), nil to disable
FieldStringStyle for string field values, nil to disable
FieldTimeStyle for time.Time field values, nil to disable
KeyDefaultStyle for field key names without a per-key override, nil to disable
KeysField key name -> value style override
LevelsPer-level label style (e.g. “INF”, “ERR”), nil to disable
MessagesPer-level message text style, nil to disable
PercentGradientGradient color stops for Percent fields
QuantityThresholdsQuantity unit -> magnitude-based style thresholds
QuantityUnitsQuantity unit string -> style override
SeparatorStyle for the separator between key and value
SymbolsPer-level symbol style
TimestampStyle for the timestamp, nil to disable
ValuesTyped value -> style (uses Go equality, so bool true != string "true")

Configuration

Behavioural settings are configured via setter methods on Logger (or package-level convenience functions for the Default logger). Settings marked pkg are package-level functions only (not Logger methods):

SetterTypeDefaultScopeDescription
SetAnimationIntervaltime.Duration67msLoggerMinimum refresh interval for all animations (0 = use built-in rates)
SetDurationGradientMaxtime.Duration0pkgMax duration for Duration field gradient (0 = disabled)
SetElapsedFormatFuncfunc(time.Duration) stringnilpkgCustom format function for Elapsed fields
SetElapsedGradientMaxtime.Duration0pkgMax duration for elapsed gradient (0 = disabled)
SetElapsedMinimumtime.Durationtime.SecondpkgMinimum duration for Elapsed fields to be displayed
SetElapsedPrecisionint0pkgDecimal places for Elapsed display (0 = “3s”, 1 = “3.2s”)
SetElapsedRoundtime.Durationtime.SecondpkgRounding granularity for Elapsed values (0 to disable)
SetFieldSortSortSortNoneLoggerSort order: SortNone, SortAscending, SortDescending
SetHyperlinkColumnFormatstringnilpkgURL format for file+line+column hyperlinks
SetHyperlinkDirFormatstringnilpkgURL format for directory hyperlinks
SetHyperlinkEnabledbooltruepkgEnable/disable all hyperlink rendering
SetHyperlinkFileFormatstringnilpkgURL format for file-only hyperlinks
SetHyperlinkLineFormatstringnilpkgURL format for file+line hyperlinks
SetHyperlinkPathFormatstringnilpkgGeneric fallback URL format for any path
SetHyperlinkPresetstring""pkgConfigure all format slots via named preset (returns error)
SetPercentFormatFuncfunc(float64) stringnilpkgCustom format function for Percent fields
SetPercentReverseGradientboolfalsepkgReverse the percent gradient (green=0%, red=100%)
SetPercentPrecisionint0pkgDecimal places for Percent display (0 = “75%”, 1 = “75.0%”)
SetPercentMaximumfloat641.0pkgPercent input maximum (1 = fractions 0–1, 100 = 0–100)
SetQuantityUnitsIgnoreCasebooltruepkgCase-insensitive quantity unit matching
SetSeparatorTextstring"="LoggerKey/value separator string

Each Threshold pairs a minimum value with style overrides:

type ThresholdStyle struct {
  Number Style // Override for the number segment (nil = keep default).
  Unit   Style // Override for the unit segment (nil = keep default).
}

type Threshold struct {
  Value float64        // Minimum numeric value (inclusive) to trigger this style.
  Style ThresholdStyle // Style overrides for number and unit segments.
}

Thresholds are evaluated in descending order - the first match wins:

styles.QuantityThresholds["ms"] = style.Thresholds{
  {Value: 5000, Style: style.ThresholdStyle{Number: redStyle, Unit: redStyle}},
  {Value: 1000, Style: style.ThresholdStyle{Number: yellowStyle, Unit: yellowStyle}},
}

Value styles only apply at Info level and above by default. Use SetFieldStyleLevel to change the threshold.

Per-Level Message Styles

Style the log message text differently for each level:

clog.SetStyles(&style.Config{
  Messages: style.LevelMap{
    clog.LevelError: new(lipgloss.NewStyle().Foreground(lipgloss.Color("1"))),
    clog.LevelWarn:  new(lipgloss.NewStyle().Foreground(lipgloss.Color("3"))),
  },
})

Use style.DefaultMessages() to get the defaults (unstyled for all levels).

Use style.DefaultValues() to get the default value styles (true=green, false=red, nil=grey, ""=grey).

Use style.DefaultPercentGradient() to get the default red → yellow → green gradient stops used for Percent fields.

Percent Gradient Direction

By default the Percent gradient runs red (0%) → yellow (50%) → green (100%) - useful when a higher value is better (e.g. battery, health score). For metrics where a lower value is better (CPU usage, disk usage, error rate), reverse the gradient:

// Global: all Percent fields
clog.SetPercentReverseGradient(true)

// Per-field: just this Percent field, regardless of the global setting
clog.Info().
  Percent("cpu", 0.92, percent.WithReverseGradient()).
  Percent("battery", 0.85).
  Msg("System status")
// "cpu" renders red at 92%, "battery" renders green at 85%

percent.WithReverseGradient() is a percent.Option passed directly to Event.Percent. It toggles the package-level setting for that field - so if the gradient is already reversed, percent.WithReverseGradient() flips it back to normal. This makes it easy to mix metrics with different semantics on the same log line regardless of the global default.

Duration Gradient

Color Duration fields on a gradient that transitions from green (fast) through yellow to red (slow). Set a max duration to enable the gradient - duration values are mapped onto 0→max, clamping at max:

clog.SetDurationGradientMax(20 * time.Second)

When active, the gradient overrides FieldDurationNumber / FieldDurationUnit and colors the entire formatted string. When the max is 0 (the default) or DurationGradient stops are nil, the existing number/unit split styling is used.

Use style.DefaultElapsedGradient() to get the default green → yellow → red gradient stops (shared with Elapsed by default).

The DurationGradientMode field controls transition style - see Gradient Mode below.

Elapsed Gradient

Color elapsed-time fields on a gradient that transitions from green (fast) through yellow to red (slow). Set a max duration to enable the gradient - elapsed values are mapped onto 0→max, clamping at max:

clog.SetElapsedGradientMax(30 * time.Second)

When active, the gradient overrides FieldElapsedNumber / FieldElapsedUnit and colors the entire formatted string. When the max is 0 (the default) or ElapsedGradient stops are nil, the existing number/unit split styling is used.

Use style.DefaultElapsedGradient() to get the default green → yellow → red gradient stops used for Elapsed fields (same stops as DurationGradient by default).

Gradient Mode

Both DurationGradientMode and ElapsedGradientMode control how colors transition between stops:

ModeDescription
style.GradientFadeSmooth interpolation between stops (default)
style.GradientStepDiscrete color jumps at stop boundaries

Fade blends smoothly between adjacent color stops using perceptually uniform CIE-LCh interpolation. Step uses the color of the last stop whose position is ≤ the current value - no blending.

clog.SetStyles(&style.Config{ElapsedGradientMode: style.GradientStep})

Custom Stops

clog.SetStyles(&style.Config{
  ElapsedGradient: []style.ColorStop{
    {Position: 0, Color: colorful.Color{R: 0, G: 1, B: 0}},   // green
    {Position: 1, Color: colorful.Color{R: 1, G: 0, B: 0}},   // red
  },
})

Format Hooks

Override the default formatting for Elapsed and Percent fields:

// Custom elapsed format: truncate to whole seconds
clog.SetElapsedFormatFunc(func(d time.Duration) string {
  return d.Truncate(time.Second).String()
})

// Custom percent format: "75/100" instead of "75%"
clog.SetPercentFormatFunc(func(v float64) string {
  return fmt.Sprintf("%.0f/100", v)
})

When set to nil (the default), the built-in formatters are used (formatElapsed with SetElapsedPrecision for elapsed, strconv.FormatFloat with SetPercentPrecision + “%” for percent). Custom percent format functions receive the display percentage (0–100), not the raw stored value.

Field Sort Order

Control the order fields appear in log output. By default fields preserve insertion order.

// Sort fields alphabetically by key
clog.SetFieldSort(clog.SortAscending)

// Or reverse alphabetical
clog.SetFieldSort(clog.SortDescending)
ConstantDescription
SortNonePreserve insertion order (default)
SortAscendingSort fields by key A→Z
SortDescendingSort fields by key Z→A
clog.Info().
  Str("zoo", "animals").
  Str("alpha", "first").
  Int("count", 42).
  Msg("Sorted")
// SortNone:       INF ℹ️ Sorted zoo=animals alpha=first count=42
// SortAscending:  INF ℹ️ Sorted alpha=first count=42 zoo=animals
// SortDescending: INF ℹ️ Sorted zoo=animals count=42 alpha=first