ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 8 · 4లో పాఠం 2కోర్సులో 30/32~12 min
మాడ్యూల్ పాఠాలు (2/4)

అపాచీ లాగ్ పార్సర్

Apache logs (combined format) have a precise structure:

Code
127.0.0.1 - - [10/Oct/2024:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 1234

A pattern with named groups extracts every field in one shot:

Code
^(?<ip>\d+\.\d+\.\d+\.\d+)\s+\S+\s+\S+\s+\[[^\]]+\]\s+"(?<metodo>\w+)\s+(?<path>\S+)\s+\S+"\s+(?<status>\d+)\s+(?<size>\d+)

It looks monstrous, but it's the concatenation of simple patterns:

  • (?<ip>\d+\.\d+\.\d+\.\d+) -- the source IPv4.
  • \s+\S+\s+\S+ -- the user IDs (usually -).
  • \[[^\]]+\] -- the date in square brackets.
  • "(?<metodo>\w+)\s+(?<path>\S+)\s+\S+" -- the request in quotes, with HTTP method, path and version.
  • (?<status>\d+)\s+(?<size>\d+) -- status code and bytes sent.

General strategy

  1. Start from the real line and pin the delimiters: spaces, quotes, brackets.
  2. Between delimiters, identify the type of token (IP, word, number).
  3. Wrap in named groups only what you need to extract.

Parsing complex logs

In log parsing, fields are usually space-separated except when they contain quoted strings or brackets (e.g. the user-agent). Using excluding character classes instead of the wildcard dot prevents merging multiple columns incorrectly.

Try it

వ్యాయామం#regex.m8.l2.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Extract the IPv4 at the start of every log line as a named group `ip`.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Anchor to the start of the line with ^ and the m flag, and wrap the IP in (?<ip>...).

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Review exercise

వ్యాయామం#regex.m8.l2.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Extract the HTTP method and path of the request between quotes, as groups `metodo` and `path`.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Open with ", then (?<metodo>\w+)\s+(?<path>\S+).

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Additional challenge

వ్యాయామం#regex.m8.l2.e3
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Extract only the date and time part enclosed in brackets in logs, e.g. `10/Oct/2024:13:55:36 +0000`, as a named group `timestamp`.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Use \[(?<timestamp>[^\]]+)\] to prevent the engine from matching past the closing bracket.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది