Developer and Text Tools

How to Parse Logs into Fields with Grok Patterns

How Grok patterns turn raw log lines into structured fields for Elasticsearch, Logstash, Fluent Bit and Loki, with a step-by-step method and pitfalls.

Saizul Amin
Saizul Amin2 Nov 2026 · 3 min read
How to Parse Logs into Fields with Grok Patterns

How to Parse Logs into Fields with Grok Patterns

Raw log lines are written for humans. Searching them for "everything slower than one second from the payments service" requires fields: a timestamp, a level, a service name, a duration as a number. Turning text into fields is called parsing, and in the Elastic world it is usually done with Grok. This guide explains Grok and a reliable way to build patterns, with the free log parser pattern builder that creates them from clicks.

Grok in one sentence

Grok is a library of named regular expressions. Instead of writing a pattern for an IPv4 address yourself, you write %{IP:client}, which means "match an IP address and store it in a field called client". Hundreds of patterns come with Logstash: TIMESTAMP_ISO8601, LOGLEVEL, INT, NUMBER, WORD, NOTSPACE, QUOTEDSTRING, URIPATHPARAM, GREEDYDATA and more.

Start from real lines

Collect five to ten real log lines, including unusual ones: an error with a stack trace header, a line with a missing value, a very long message. Patterns built from one tidy example tend to fail on the second line in production.

Work from left to right

Read the line from the start and decide for each part whether it is a value you want, a value you need to skip, or fixed text. A typical application log might be:

2026-11-02T10:15:32Z WARN user=42 took 153 ms

That gives a timestamp, a level, the literal "user=", an integer, the literal "took", another integer and the literal "ms". The Grok pattern follows the same order.

Choose the narrowest pattern that always works

WORD matches letters, digits and underscores only, so it fails on "web-01". NOTSPACE accepts anything up to the next space and is a safer default for identifiers. DATA matches as little as possible and GREEDYDATA as much as possible; keep GREEDYDATA for the free-text message at the end of the line, where it belongs.

Store numbers as numbers

Add :int or :float to a field, as in %{INT:duration_ms:int}, so that it is indexed as a number. Otherwise you cannot compute averages or filter by "greater than".

Allow flexible spacing

Many logs pad columns with spaces, for example syslog dates with one or two spaces before the day. Using \s+ between fields instead of a single literal space avoids failures on alignment.

Parse the time

After Grok extracts the timestamp as text, a date step in Logstash or the ingest pipeline converts it into the event's real time. Without it, events are stored with the time they arrived, which can be minutes later.

Test on everything

Run the pattern against all sample lines and look at every non-match. Each failure points to one field whose pattern is too narrow. Fix it and run again until all lines match.

Other pipelines, same idea

Fluent Bit uses regular expressions with named groups, Vector can call parse_grok directly, and Loki's LogQL has a faster pattern parser that uses placeholders like <level>. Once you know which parts are fields, the same structure can be written for any of them.

Performance tips

Anchor patterns to the start of the line, avoid several DATA fields in a row, and put the most common log format first when you have alternatives. Grok failures are expensive at high volume, so tag unparsed lines and monitor how many there are.

Limits

Grok works line by line. Multi-line events such as Java stack traces must be joined first in your log shipper. When the application can write structured JSON logs directly, that is more reliable than any parser and worth the change.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages