Skip to main content

LogQL Template Functions

line_format and label_format templates use Go text/template syntax. Extracted labels are the pipeline variables ({{ .status }}), and the functions below are available inside the template.

{job="app"} | json | line_format "{{ .method | upper }} {{ .path }} → {{ .status }}"

Pipe values through functions with |, or call them directly: {{ div .duration 1000 }}.


Line and timestamp​

FunctionDescription
__line__The original log line, unmodified
__timestamp__The log line's timestamp as time.Time
| line_format "{{ __timestamp__ }} {{ __line__ }}"

Strings​

FunctionSignatureNotes
lower / upper(s)Case conversion
title(s)Title Case
trim(s)Trim whitespace both ends
trimAll(chars, s)Trim the given cutset both ends
trimPrefix / trimSuffix(fix, s)Trim one end
replace(old, new, s)Replace every occurrence
substr(start, end, s)Substring by index
repeat(n, s)Repeat s n times
indent / nindent(spaces, s)Indent each line; nindent prepends a newline
alignLeft / alignRight(width, s)Pad or truncate to width
printf(format, args…)Go-style formatting
default(fallback, s)fallback when s is empty
b64enc / b64dec(s)Base64
urlencode / urldecode(s)URL encoding
| line_format `{{ alignRight 8 .status }} {{ .path | default "/" }}`

Tests​

FunctionSignature
contains(substr, s) bool
hasPrefix(prefix, s) bool
hasSuffix(suffix, s) bool
| line_format "{{ if hasPrefix \"/api\" .path }}API {{ end }}{{ .path }}"

Regex​

FunctionSignatureNotes
regexReplaceAll(regex, s, repl)repl may reference groups ($1)
regexReplaceAllLiteral(regex, s, repl)No group expansion
count(regex, s) intNumber of matches
| line_format `{{ regexReplaceAll "[0-9]+" .path "«id»" }}`

Numbers​

Integer forms: add sub mul div mod max min. Float forms: addf subf mulf divf modf maxf minf. Rounding: ceil floor round(x, places). Conversion: int float64 toFloat64.

| json | line_format "{{ div .duration_ns 1000000 }}ms"

Dates​

FunctionDescription
nowCurrent time
date / toDate / toDateInZoneFormat / parse with a Go layout
unixEpoch / unixEpochMillis / unixEpochNanosEpoch from a time.Time
unixToTimetime.Time from an epoch string
| line_format "{{ .ts | toDate \"2006-01-02T15:04:05Z\" | date \"15:04:05\" }}"

Parsing helpers​

FunctionDescription
duration / duration_secondsHumanised duration ("2h45m") → seconds as float
bytesNumber → readable string ("83 MB")
fromJsonDecode a JSON string into a value you can index
| logfmt | line_format "{{ (fromJson .payload).userId }}"