Grok Pattern Tester with Failure Diagnosis
Operator: Abdulla Al Fahad. How tools are built and checked
COMBINEDAPACHELOG run over four log lines parses two of them into client address, verb, response code and byte count, and rejects the other two for different reasons: one is missing its referrer and user agent, one is not an access log line at all. A plain no-match result says only that something is wrong. This tester also names the first piece of the pattern where matching stops, so the third line reports that it ends before the quoted referrer, and the fourth reports that it already fails at the timestamp bracket.
Key features
- 71 bundled patterns for numbers, words, IPs, hosts, paths, URIs, dates, syslog, log levels and Apache or nginx access logs
- %{SYNTAX}, %{SYNTAX:field} and %{SYNTAX:field:type} with int, float and boolean conversion, plus (?<name>regex) named captures
- Several patterns at once, tried in order, with the matching pattern number shown for each line
- Your own NAME regex definitions, which override bundled ones of the same name
- A fields table per line, JSON and CSV export, and the compiled regular expression for pasting elsewhere
- A worker with a five-second limit and a Cancel button, so a pattern that backtracks badly cannot freeze the page
How %{SYNTAX:field:type} becomes a regular expression
Expansion is recursive: %{COMBINEDAPACHELOG} contains %{COMMONAPACHELOG}, which contains %{IPORHOST:clientip} and so on, and a named reference anywhere in that tree produces a field. References that name no field become non-capturing groups. A pattern that refers to itself is reported with the cycle, nesting stops at 24 levels, and the expanded pattern is limited to 200,000 characters. Field names written as [http][method] are flattened to http.method. When one field name appears twice in a pattern the values are collected into a list, as Logstash does.
Typed captures convert the text. Logstash documents only int and float; this tester accepts integer, long, double and number as aliases and adds boolean (true, 1, yes and y), which is its own extension, so a pattern relying on it will not convert in Logstash itself. int, integer and long use parseInt, and float, double and number use parseFloat. Other type names leave the value as a string and add a note. Matching is unanchored like Logstash: the pattern may match anywhere in the line, so add ^ and $ when the whole line must conform. Up to 5,000 lines of 20,000 characters each are tested, and 2,000,000 characters in all.
How it works
Grok is a layer over Oniguruma, the regular expression engine of Ruby, and browsers run a different dialect, so each fragment is translated first. Atomic groups (?>...) and possessive quantifiers lose their atomicity, \h becomes a hex-digit class, \A, \z and \Z become anchors, POSIX classes such as [[:alpha:]] become ranges, a leading (?i) becomes the i flag, and (?<name>...) groups are renamed internally. The translation notes are shown, because dropping atomicity can change how much backtracking a match needs, though not what it matches. Lookbehind works as it is.
When to run it
- A new application logs 'ts level logger: message' lines, and a custom pattern %{TIMESTAMP_ISO8601:ts} %{LOGLEVEL:level} %{JAVACLASS:logger}: %{GREEDYDATA:msg} is tested on 200 sample lines to find the 14 whose logger name contains a hyphen.
- A Logstash filter has two patterns for two log versions; they are pasted one per line and the pattern number column shows which version each line took.
- An access log field is switched from %{NUMBER:bytes} to %{NUMBER:bytes:int}, and the CSV export confirms that the column now holds numbers and that dashes for empty responses become blanks.
Running a check
- Enter one pattern per line, optionally defining your own names in the custom box.
- Paste sample lines, or drop a log file; only the first 5,000 lines are used.
- Press Test pattern and filter the results to No match.
- Read the stop point on a failing line, adjust the pattern, and test again.
- Export JSON or CSV when the fields look right, or copy the compiled regular expression.
Example
Pattern and a line that fails
%{COMBINEDAPACHELOG}
1: 203.0.113.9 - alice [10/Oct/2024:13:55:36 +0000] "GET /api/users?id=7 HTTP/1.1" 200 2326 "https://example.com/" "Mozilla/5.0"
2: 2001:db8::1 - - [10/Oct/2024:13:55:37 +0000] "POST /login HTTP/2.0" 302 - "-" "curl/8.0"
3: 203.0.113.9 - alice [10/Oct/2024:13:55:38 +0000] "GET /x HTTP/1.1" 200 2326Result
Line 1 matches: clientip 203.0.113.9, verb GET, response 200, bytes 2326.
Line 2 matches: clientip 2001:db8::1, verb POST, response 302, no bytes (the dash is not a number).
Line 3 does not match: it stops before " %{QS:referrer}" after matching everything up to the byte count 2326.Limits
This is a JavaScript regular expression, not Ruby's. Features that exist only in Oniguruma, such as \G, \K and recursion, are not translated and fail to compile, and inline flag groups like (?i:...) work only in browsers recent enough to support regular expression modifiers.
A bundled pattern is written from the common grok definitions but is not guaranteed to be byte-identical to the library in your Logstash or Elasticsearch version; check unusual inputs such as IPv6 and e-mail addresses on your own system.
Lowercase names such as %{notSpace:path} resolve because the lookup falls back to upper case, but Datadog's regex() and date() matchers and its filters such as nullIf are not supported.
The five-second limit covers the whole run, not each line. If it is reached, the partial results are discarded and a message tells you the run was abandoned.
The stop point is a diagnosis, not proof: when a pattern has alternatives or optional parts, the first piece that fails to match is a good lead, not always the real cause.
- Test the pattern against lines that should fail as well: an unanchored pattern can match inside a line it was never meant for.
- Put the most specific pattern first when several are listed, since the first one that matches wins and later ones are not tried on that line.
Common errors and how to fix them
- "Custom pattern line 2 needs a NAME and a regex separated by a space"
- Each custom line is a name, a space and the expression, as in ORDERID [A-Z]{3}-\d+. Empty lines and # comment lines are skipped.
- "Unknown pattern %{NOPE}"
- The name is not in the bundled list or your custom definitions. The message suggests names that start the same way; define it in the custom box as NAME regex.
- "Pattern A refers to itself (A > B > A)"
- Two custom patterns reference each other. Rewrite one of them so the chain ends in plain regular expression text.
Reading the stop point
When no pattern matches a line, the tester splits each pattern at its %{...} references and opens the library patterns that are plain sequences, such as COMBINEDAPACHELOG. It then tries longer and longer prefixes of the pattern anchored at the start of the line and keeps the longest that matches. The next piece of the pattern, joined to a preceding separator such as a space, is reported with the text that did match.
In the access-log example the third line matches up to the byte count and stops before the space and %{QS:referrer}: the line is a common-log line, not a combined-log line. The fourth stops after "not an access" at the literal bracket and %{MONTHDAY} of the timestamp. Diagnosis runs for the first 200 failing lines.
Why a worker runs the match
A grok pattern such as %{DATA:a}.*%{DATA:b}.*%{DATA:c}x on a long line that does not end in x can take exponential time in a backtracking engine, and a page cannot be interrupted from inside its own regular expression. The matching therefore runs in a separate worker that the page terminates after five seconds, with a message that explains the usual causes: nested repeats, and greedy wildcards in front of a long literal.
Frequently asked questions
Does it match like Logstash, anchored or not?
Not anchored. The pattern can match anywhere in the line, which is how the Logstash grok filter works, so add ^ and $ if the whole line has to fit.