pygrok
raw JSON → 1.0.0 verified Mon Apr 27 auth: no python
A Python library to parse strings and extract information from structured/unstructured data using Grok patterns (similar to logstash grok). Current version is 1.0.0, with a major API redesign in v1.0.0 that breaks compatibility with older versions. Release cadence is sporadic, with most updates in 2015-2016.
pip install pygrok Common errors
error AttributeError: module 'pygrok' has no attribute 'Grok' ↓
cause Using wrong import path in v1.0.0; old code uses `from pygrok.grok import Grok`.
fix
Use
from pygrok import Grok. error ImportError: No module named pygrok.grok ↓
cause Installation of old version or using outdated import with v1.0.0.
fix
Reinstall with
pip install pygrok (v1.0.0) and use from pygrok import Grok. Warnings
breaking v1.0.0 introduced a completely new API. Old code using `Grok(pattern)` and `grok.match(text)` is no longer compatible. The new API uses `Grok('%{PATTERN:name}')` and returns a dict from `match()`. No support for `%{PATTERN}` (without a field name) in v1.0.0? Actually v1.0.0 still supports it. But the class `Grok` is now the main entry point. ↓
fix Upgrade to v1.0.0 and use `from pygrok import Grok`.
deprecated Older versions (0.x) used `pygrok.grok.Grok`. This import path is removed in v1.0.0. ↓
fix Change to `from pygrok import Grok`.
gotcha The `match()` method returns `None` if no match, not an empty dict. Always check for `None` before accessing results. ↓
fix Use `result = grok.match(text); if result: ...`.
Imports
- Grok wrong
from pygrok.grok import Grokcorrectfrom pygrok import Grok
Quickstart
from pygrok import Grok
grok = Grok('%{IP:client_ip}')
text = '192.168.0.1'
print(grok.match(text))