Ravindra BagaleCourses & study guides मराठी

Chapter 95: Logging with Loki, Elastic Stack and CloudWatch

DevOps

95.6 LogQL — selector, line filter and JSON parser

Story (fictional teaching scenario). At a fictional Indore bakery app, Kavya and Omar from Doha first choose the lab stream, then parse its structured error records.

LogQL — selector, line filter and JSON parser
LogQL — selector, line filter and JSON parser

Why

Filtering the correct source before parsing reduces work and makes a query easier to explain.

What

  • LogQL is Loki’s query language.
  • A stream selector chooses streams by labels inside braces.
  • A line filter keeps or removes lines by their text.
  • A parser extracts fields from a line. | json parses JSON.
  • __error__ is a pipeline error label; parser failures must not silently become valid results.

How

  1. Choose the narrow origin labels first.

  2. Use a cheap line filter when useful.

  3. Parse the needed format and inspect parser errors.

  4. Filter extracted fields after parsing.

Example 1: Search structured errors

  1. Read the command or request.
{app="bakery", env="lab"}
| json
| __error__=""
| level="ERROR"
  1. Read the result.

Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)

Fictional matching events: req-demo-003 and req-demo-004.
  1. Line by line: Braces choose streams with both origin labels. | json extracts body fields. The empty error filter excludes parser failures; inspect rejected lines separately. level="ERROR" selects the two fake error events if all six fixture events were ingested with these labels. No Loki engine was run.

Example 2: Text containment is not a structured comparison

  1. Read the command or request.
{app="bakery", env="lab"} |= "ERROR"
  1. Read the result.

Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)

Any selected log line containing the case-sensitive substring ERROR can match.
  1. Line by line: |= searches text, including an unrelated field that happens to contain the word. It is not identical to checking parsed level. Do not confuse stream-label matchers before the pipe with extracted fields after parsing.

Ravindra Bagale's Tip

A text match and a field comparison answer different questions.

Did you understand? Let us try it once more.