Skip to content

Lesson 12 of 13 · 20m

Grok, dissect, and the Case for JSON Logs

Named regex patterns in Logstash, Datadog, and Loki; dissect for fixed shapes; and why emitting JSON from the app beats maintaining grok rules.

Every log line your systems emit is already structured — you wrote the format in an f-string years ago — but to the tools that consume it, it's a flat string somebody has to re-parse. The industry solved this two ways: Grok, which is regex with names, and the much better answer, emitting JSON in the first place.

Grok is regex with a shared vocabulary

Logstash's grok filter ships hundreds of named patterns — %{IPORHOST}, %{HTTPDATE}, %{NUMBER}, %{QS} — that compile down to ordinary regexes. %{COMBINEDAPACHELOG} alone unpacks an entire nginx access line:

%{IPORHOST:clientip} %{HTTPDUSER:ident} %{HTTPDUSER:auth} \[%{HTTPDATE:timestamp}\]
"(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})"
%{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent}

The name:field suffix captures into field. Your own formats get one-off rules:

%{TIMESTAMP_ISO8601:ts} %{LOGLEVEL:level} \[%{DATA:logger}\] %{GREEDYDATA:msg}

Grok runs in Logstash (filter { grok { match => ... } }), in Datadog log pipelines (parsing rules use the same %{matcher:field} idea with slightly different matcher names — %{word}, %{notSpace}, %{date("...")}), in Grafana Loki's pattern parser at query time (| pattern "<ip> <_> <status>"), and in Vector's parse_grok remap function. Filebeat ships a dissect processor for the delimiter side.

A Logstash config for a custom app line looks like:

filter {
  grok {
    match => { "message" => "%{TIMESTAMP_ISO8601:ts} %{LOGLEVEL:level} \[%{DATA:logger}\] %{GREEDYDATA:msg}" }
  }
}

Grok is still regex under the hood

Everything the catastrophic-backtracking lesson teaches applies here. %{GREEDYDATA} is .*. Chaining %{DATA}:%{DATA}:%{DATA} over attacker-controlled input is the same overlapping-alternation shape that took Cloudflare down. Logstash grok compiles to Joni, a Java port of Oniguruma — a backtracking engine — so a bad rule on a busy input pin stalls a pipeline the way a bad WAF rule stalled an edge. Keep rules tight: concrete token patterns (%{NUMBER}, %{IP}, bounded %{DATA} where you can), never stacked wildcards.

dissect: the fast path for fixed shapes

Logstash's dissect filter parses lines by delimiter map instead of regex: %{clientip} %{ident} %{auth} [%{timestamp}] treats everything between literals as a field. No backtracking, dramatically cheaper, but no alternation and no quantifiers. When the format is fixed "field field field," dissect beats grok on throughput by an order of magnitude.

The catch: dissect can't express "the request is three sub-fields" or "this field is optional." The moment a line's shape varies, you're back to grok or back to emitting JSON.

Both live downstream of the same problem: the app wrote a flat string and somebody else has to reconstruct its fields. Grok and dissect reconstruct; the emitter could have just serialized them.

The better answer: emit JSON

The senior move is to never write grok at all:

import json, time
print(json.dumps({"ts": time.time(), "level": "error", "logger": "billing",
                  "msg": "charge failed", "stripe_id": "ch_3Qx"}))

One print gives you a line that jq, Loki's | json parser, Datadog, Vector, Fluent Bit, and every warehouse ingests natively. The app controls the schema, fields arrive typed (numbers as numbers), and nobody maintains a pattern that must be updated when the log format changes — because the format can't drift. It is the data.

Field notes

  • logfmt — level=error msg="charge failed" — is the middle ground: human-readable, machine-parseable without regex, supported natively by Loki and Vector.
  • A grok rule is a schema maintained in a config file. When the app starts emitting JSON, delete it.
  • Whatever you emit, jq at query time answers questions without a pipeline stage — the .items[] pipelines in the toolkit lesson.
  • Keep grok for other people's formats you can't change: vendor logs, syslog, appliances. For your own services, you're the emitter — emit structure.
  • Grok debuggers exist (Heroku's and others ship online testers); test a rule against real captured lines, never imagined ones.

Keep reading

Related articles from the SudoSecurity blog.