loglenz 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loglenz-0.1.0/LICENSE +21 -0
- loglenz-0.1.0/PKG-INFO +570 -0
- loglenz-0.1.0/README.md +551 -0
- loglenz-0.1.0/loglens/__init__.py +6 -0
- loglenz-0.1.0/loglens/__main__.py +10 -0
- loglenz-0.1.0/loglens/cli.py +199 -0
- loglenz-0.1.0/loglens/display.py +246 -0
- loglenz-0.1.0/loglens/export.py +64 -0
- loglenz-0.1.0/loglens/filters.py +185 -0
- loglenz-0.1.0/loglens/parser.py +83 -0
- loglenz-0.1.0/loglens/presets.py +41 -0
- loglenz-0.1.0/loglens/stats.py +149 -0
- loglenz-0.1.0/loglens/suggest.py +186 -0
- loglenz-0.1.0/loglenz.egg-info/PKG-INFO +570 -0
- loglenz-0.1.0/loglenz.egg-info/SOURCES.txt +25 -0
- loglenz-0.1.0/loglenz.egg-info/dependency_links.txt +1 -0
- loglenz-0.1.0/loglenz.egg-info/entry_points.txt +2 -0
- loglenz-0.1.0/loglenz.egg-info/requires.txt +4 -0
- loglenz-0.1.0/loglenz.egg-info/top_level.txt +1 -0
- loglenz-0.1.0/pyproject.toml +33 -0
- loglenz-0.1.0/setup.cfg +4 -0
- loglenz-0.1.0/tests/test_cli.py +111 -0
- loglenz-0.1.0/tests/test_export.py +62 -0
- loglenz-0.1.0/tests/test_filters.py +98 -0
- loglenz-0.1.0/tests/test_parser.py +67 -0
- loglenz-0.1.0/tests/test_stats.py +71 -0
- loglenz-0.1.0/tests/test_suggest.py +31 -0
loglenz-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Syed Abdul Ali Shah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
loglenz-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: loglenz
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A terminal log parser that turns messy log files into clean, filterable, exportable tables.
|
|
5
|
+
Author-email: Syed Abdul Ali Shah <syedabdulalishah.786@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shah541-g/Agentic-AI/tree/main/1-%20PY4E/Worked%20Examples/WE%201%20-%20LogLens
|
|
8
|
+
Keywords: log,parser,regex,cli,nginx,syslog
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Topic :: System :: Logging
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: rich>=13.0.0
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# LogLens π
|
|
21
|
+
|
|
22
|
+
> A fast, lightweight Python command-line tool that turns messy log files into clean, filterable, exportable tables using regex presets, custom regex, and automatic regex suggestion.
|
|
23
|
+
|
|
24
|
+
[](https://www.python.org/)
|
|
25
|
+
[](LICENSE)
|
|
26
|
+
[]()
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## π Table of Contents
|
|
31
|
+
|
|
32
|
+
1. [Overview & Problem Statement](#overview--problem-statement)
|
|
33
|
+
2. [Regex Practitioner's Mental Model (Read This First!)](#regex-practitioners-mental-model-read-this-first)
|
|
34
|
+
3. [Architecture & Design Principles](#architecture--design-principles)
|
|
35
|
+
4. [Installation & Setup](#installation--setup)
|
|
36
|
+
5. [CLI Reference & Options](#cli-reference--options)
|
|
37
|
+
6. [Built-in Presets Explained (Regex Breakdown)](#built-in-presets-explained-regex-breakdown)
|
|
38
|
+
7. [Comprehensive Usage Examples with Real Terminal Outputs](#comprehensive-usage-examples-with-real-terminal-outputs)
|
|
39
|
+
- [Basic Parsing with Built-in Presets](#1-basic-parsing-with-built-in-presets)
|
|
40
|
+
- [Filtering Rows with `--where`](#2-filtering-rows-with---where)
|
|
41
|
+
- [Combining Multiple Filters (AND Logic)](#3-combining-multiple-filters-and-logic)
|
|
42
|
+
- [Inspecting Malformed / Unmatched Lines](#4-inspecting-malformed--unmatched-lines)
|
|
43
|
+
- [Statistical Summaries & ASCII Bar Charts](#5-statistical-summaries--ascii-bar-charts)
|
|
44
|
+
- [Exporting to CSV and JSON](#6-exporting-to-csv-and-json)
|
|
45
|
+
- [Streaming Logs via Stdin (UNIX Pipe / PowerShell)](#7-streaming-logs-via-stdin-unix-pipe--powershell)
|
|
46
|
+
- [Custom Regex with Named Capture Groups](#8-custom-regex-with-named-capture-groups)
|
|
47
|
+
- [Automatic Regex Suggestion from a Sample Line](#9-automatic-regex-suggestion-from-a-sample-line)
|
|
48
|
+
8. [Module Reference](#module-reference)
|
|
49
|
+
9. [Running Tests](#running-tests)
|
|
50
|
+
10. [Dedicated Regex Guide](#dedicated-regex-guide)
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Overview & Problem Statement
|
|
55
|
+
|
|
56
|
+
Developers, students, and system administrators inspect Nginx, Apache, Syslog, and custom application logs daily. Reading raw logs by eye is exhausting, and manually writing complex `awk`/`sed`/`grep` pipelines is error-prone.
|
|
57
|
+
|
|
58
|
+
**LogLens** solves this by providing:
|
|
59
|
+
- **Instant Structured Tables:** Automatically parses raw lines into colored tables in your terminal.
|
|
60
|
+
- **Dynamic Schema via Named Groups:** Regex capture group names automatically become table headers and CSV/JSON fields.
|
|
61
|
+
- **Zero Heavy Infrastructure:** No ELK stack, no Splunk, no local database, and no web server required.
|
|
62
|
+
- **Streaming & Memory Safe:** Operates via Python generators (`yield`) line by lineβnever reads entire multi-gigabyte log files into RAM.
|
|
63
|
+
- **Privacy & Offline:** 100% local, no network requests.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Regex Practitioner's Mental Model (Read This First!)
|
|
68
|
+
|
|
69
|
+
If you know regular expressions, you already know 90% of LogLens! Here is how your regex expertise maps directly to LogLens concepts:
|
|
70
|
+
|
|
71
|
+
### 1. Named Capture Groups Define Table Columns
|
|
72
|
+
In standard PCRE and Python regular expressions, you define named groups with:
|
|
73
|
+
```regex
|
|
74
|
+
(?P<column_name>pattern)
|
|
75
|
+
```
|
|
76
|
+
Whenever a log line matches your regex, LogLens calls Python's `match.groupdict()`. Every named group becomes a **column** in the rendered terminal table and a **field** in CSV/JSON exports:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
Regex: ^(?P<ip>\S+) \S+ \S+ \[(?P<time>[^\]]+)\] "(?P<method>[A-Z]+) (?P<path>\S+)
|
|
80
|
+
β β β β
|
|
81
|
+
Table: βββββββββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ
|
|
82
|
+
β ip β time β method β path β
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 2. Non-Capturing Groups Structure Without Cluttering
|
|
86
|
+
Use `(?:...)` when you need grouping or alternation without creating unwanted columns:
|
|
87
|
+
```regex
|
|
88
|
+
(?:\[(?P<pid>\d+)\])?
|
|
89
|
+
```
|
|
90
|
+
Here, `pid` becomes a table column, while the outer brackets and optional marker `?` do not create redundant table columns.
|
|
91
|
+
|
|
92
|
+
### 3. Anchors & Exact Line Matching
|
|
93
|
+
All built-in presets use `^` (start of line) and `$` (end of line). If a single character fails to match, LogLens flags the line as **unmatched** rather than silently truncating or yielding broken columns.
|
|
94
|
+
|
|
95
|
+
### 4. How `--where` Filters Regex Groups
|
|
96
|
+
Filter expressions like `--where "status>=500"` or `--where "path~admin"` operate directly on the strings captured by your named groups:
|
|
97
|
+
- **`=` and `!=`**: Case-insensitive string equality (or mathematical equality if numeric).
|
|
98
|
+
- **`>` `<` `>=` `<=`**: Numerical comparison if both sides are numbers (e.g. `"1000"` is greater than `"500"`), otherwise lexicographical.
|
|
99
|
+
- **`~`**: **Full Regex Search!** `--where "path~admin"` executes `re.search("admin", row["path"], re.IGNORECASE)`.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Architecture & Design Principles
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
ββββββββββββββββββββββββββ
|
|
107
|
+
β Raw Log (File / Stdin) β
|
|
108
|
+
βββββββββββββ¬βββββββββββββ
|
|
109
|
+
β (Line-by-line streaming)
|
|
110
|
+
βΌ
|
|
111
|
+
ββββββββββββββββββββββββββ
|
|
112
|
+
β parser.parse_lines() β ββββ PRESETS / Custom Regex
|
|
113
|
+
βββββββ¬βββββββββββββ¬ββββββ
|
|
114
|
+
(Matched rows) β β (Unmatched lines, max 1000 kept)
|
|
115
|
+
βΌ βΌ
|
|
116
|
+
ββββββββββββββββββββ ββββββββββββββββββββββββ
|
|
117
|
+
β filters.py β β display.py β
|
|
118
|
+
β apply_filters() β β show_unmatched() β
|
|
119
|
+
ββββββββββ¬ββββββββββ ββββββββββββββββββββββββ
|
|
120
|
+
β
|
|
121
|
+
ββββββββββββββΌβββββββββββββ
|
|
122
|
+
βΌ βΌ βΌ
|
|
123
|
+
ββββββββββββββββββββββββββββββββββββββββββ
|
|
124
|
+
β display.py ββ stats.py ββ export.py β
|
|
125
|
+
β show_table ββ show_stats ββ .csv/.json β
|
|
126
|
+
ββββββββββββββββββββββββββββββββββββββββββ
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
1. **Simple:** One single command with intuitive flags and helpful defaults.
|
|
130
|
+
2. **Memory Safe:** Uses Python generator functions (`Iterator[tuple[dict | None, str]]`). Memory usage remains constant even when analyzing 50GB log files.
|
|
131
|
+
3. **Debug Friendly:** Unmatched lines are never dropped. `--show-unmatched` preserves raw line content and 1-based original file line numbers.
|
|
132
|
+
4. **Minimal Dependencies:** Built strictly using the Python Standard Library (`re`, `csv`, `json`, `argparse`, `collections`, `datetime`) plus `rich` for formatting.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Installation & Setup
|
|
137
|
+
|
|
138
|
+
### Prerequisites
|
|
139
|
+
- Python 3.10 or higher
|
|
140
|
+
- `pip`
|
|
141
|
+
|
|
142
|
+
### Step 1: Clone or Navigate to the Repository
|
|
143
|
+
```bash
|
|
144
|
+
cd "path/to/loglens"
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Step 2: Install in Editable Mode
|
|
148
|
+
```bash
|
|
149
|
+
pip install -e .
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Step 3: Verify Installation
|
|
153
|
+
```bash
|
|
154
|
+
loglens --version
|
|
155
|
+
# Output: loglens 0.1.0
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Alternatively, you can run LogLens directly without installation via Python's module runner:
|
|
159
|
+
```bash
|
|
160
|
+
python -m loglens --version
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## CLI Reference & Options
|
|
166
|
+
|
|
167
|
+
```text
|
|
168
|
+
usage: loglens [-h] [--preset {nginx,apache,syslog,generic}] [--regex REGEX]
|
|
169
|
+
[--where EXPR] [--show-unmatched] [--stats] [--out PATH]
|
|
170
|
+
[--limit N] [--suggest SAMPLE] [--version]
|
|
171
|
+
[file]
|
|
172
|
+
|
|
173
|
+
A fast CLI log parser that turns messy logs into structured tables.
|
|
174
|
+
|
|
175
|
+
positional arguments:
|
|
176
|
+
file Log file to parse (if omitted, reads from stdin).
|
|
177
|
+
|
|
178
|
+
options:
|
|
179
|
+
-h, --help Show this help message and exit.
|
|
180
|
+
--preset {nginx,apache,syslog,generic}
|
|
181
|
+
Use a pre-defined regex pattern.
|
|
182
|
+
--regex REGEX Custom regex with named capture groups, e.g. '(?P<time>\S+) (?P<level>\w+)'.
|
|
183
|
+
--where EXPR Filter parsed rows, e.g. --where 'status>=500' or --where 'method=POST' (repeatable).
|
|
184
|
+
--show-unmatched Display lines that did not match the regex pattern.
|
|
185
|
+
--stats Display summary statistics and metrics.
|
|
186
|
+
--out PATH Export parsed/filtered rows to a .csv or .json file.
|
|
187
|
+
--limit N Maximum rows to display in table output (default: 50).
|
|
188
|
+
--suggest SAMPLE Suggest a regex pattern from a sample log line and exit.
|
|
189
|
+
--version Show program version and exit.
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Built-in Presets Explained (Regex Breakdown)
|
|
195
|
+
|
|
196
|
+
Every preset in `loglens/presets.py` is an optimized compiled regular expression. Here is the exact regex anatomy for each preset:
|
|
197
|
+
|
|
198
|
+
### 1. `nginx` / `apache` (Combined Log Format)
|
|
199
|
+
```regex
|
|
200
|
+
^(?P<ip>\S+) \S+ (?P<user>\S+) \[(?P<time>[^\]]+)\] "(?P<method>[A-Z]+) (?P<path>\S+) (?P<protocol>[^"]+)" (?P<status>\d{3}) (?P<size>\d+|-) "(?P<referrer>[^"]*)" "(?P<user_agent>[^"]*)"$
|
|
201
|
+
```
|
|
202
|
+
- `^(?P<ip>\S+)`: Matches client IP address up to first space.
|
|
203
|
+
- `\S+`: Consumes the identity check (typically `-`).
|
|
204
|
+
- `(?P<user>\S+)`: Captures authenticated user (or `-`).
|
|
205
|
+
- `\[(?P<time>[^\]]+)\]`: Captures everything between `[` and `]` as timestamp (e.g. `10/Oct/2025:13:55:36 +0000`).
|
|
206
|
+
- `"(?P<method>[A-Z]+) (?P<path>\S+) (?P<protocol>[^"]+)"`: Parses the HTTP request line inside quotes into three named groups: `method`, `path`, and `protocol`.
|
|
207
|
+
- `(?P<status>\d{3})`: Exactly 3 digits for HTTP status code (`200`, `404`, `500`).
|
|
208
|
+
- `(?P<size>\d+|-)`: Response body size in bytes or `-`.
|
|
209
|
+
- `"(?P<referrer>[^"]*)"`: HTTP Referrer URL inside quotes.
|
|
210
|
+
- `"(?P<user_agent>[^"]*)"$`: Client User-Agent string anchored to end of line.
|
|
211
|
+
|
|
212
|
+
### 2. `syslog` (RFC 3164)
|
|
213
|
+
```regex
|
|
214
|
+
^(?P<time>\w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2}) (?P<host>\S+) (?P<process>[^\[:]+)(?:\[(?P<pid>\d+)\])?: (?P<message>.*)$
|
|
215
|
+
```
|
|
216
|
+
- `^(?P<time>\w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2})`: Captures month name, day, and time (`Oct 10 13:55:36`).
|
|
217
|
+
- `(?P<host>\S+)`: Captures the logging hostname.
|
|
218
|
+
- `(?P<process>[^\[:]+)`: Daemon/program name (e.g. `sshd`, `systemd`).
|
|
219
|
+
- `(?:\[(?P<pid>\d+)\])?`: Optional non-capturing group containing named group `pid`. Matches `[1234]` if present, or sets `pid` to `""` if absent.
|
|
220
|
+
- `: (?P<message>.*)$`: Consumes the separator and captures the rest of the line as `message`.
|
|
221
|
+
|
|
222
|
+
### 3. `generic` (Standard Application Logs)
|
|
223
|
+
```regex
|
|
224
|
+
^(?P<time>\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?)\s+\[?(?P<level>DEBUG|INFO|WARN(?:ING)?|ERROR|CRITICAL|FATAL)\]?\s+(?P<message>.*)$
|
|
225
|
+
```
|
|
226
|
+
- `^(?P<time>...)`: Matches ISO timestamps with space or `T`, and optional millisecond/microsecond fractions.
|
|
227
|
+
- `\[?(?P<level>...)\]?`: Matches severity levels with or without enclosing brackets `[INFO]` or `INFO`.
|
|
228
|
+
- `(?P<message>.*)$`: Captures the rest of the application log payload.
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Comprehensive Usage Examples with Real Terminal Outputs
|
|
233
|
+
|
|
234
|
+
### 1. Basic Parsing with Built-in Presets
|
|
235
|
+
Parse an Nginx log file and render it as a syntax-colored table:
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
loglens sample_logs/nginx.log --preset nginx --limit 5
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**Terminal Output:**
|
|
242
|
+
```text
|
|
243
|
+
Parsed Logs
|
|
244
|
+
βββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬βββββββ¬ββββββββ¬βββββββ
|
|
245
|
+
βip β user β time β methβ¦ β path β protβ¦ β statβ¦ β size β refeβ¦ β userβ¦β
|
|
246
|
+
βββββββββΌββββββββΌββββββββΌββββββββΌββββββββΌββββββββΌββββββββΌβββββββΌββββββββΌβββββββ€
|
|
247
|
+
β192.1β¦ β - β 10/Oβ¦ β GET β /indβ¦ β HTTPβ¦ β 200 β 1024 β - β Moziβ¦β
|
|
248
|
+
β β β +0000 β β β β β β β β
|
|
249
|
+
β192.1β¦ β frank β 10/Oβ¦ β POST β /apiβ¦ β HTTPβ¦ β 200 β 512 β httpβ¦ β Moziβ¦β
|
|
250
|
+
β β β +0000 β β β β β β β β
|
|
251
|
+
β192.1β¦ β - β 10/Oβ¦ β GET β /oldβ¦ β HTTPβ¦ β 301 β 0 β - β curlβ¦β
|
|
252
|
+
β β β +0000 β β β β β β β β
|
|
253
|
+
β10.0.β¦ β - β 10/Oβ¦ β GET β /imaβ¦ β HTTPβ¦ β 200 β 452β¦ β httpβ¦ β Moziβ¦β
|
|
254
|
+
β β β +0000 β β β β β β β β
|
|
255
|
+
β10.0.β¦ β - β 10/Oβ¦ β GET β /notβ¦ β HTTPβ¦ β 404 β 153 β - β Moziβ¦β
|
|
256
|
+
β β β +0000 β β β β β β β β
|
|
257
|
+
βββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄βββββββ΄ββββββββ΄βββββββ
|
|
258
|
+
Showing 5 of 17 rows
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
*(Note: In terminal windows, status codes are colored automatically: 2xx green, 3xx cyan, 4xx yellow, 5xx red).*
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
### 2. Filtering Rows with `--where`
|
|
266
|
+
Filter rows numerically (e.g. status code 500 and higher):
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
loglens sample_logs/nginx.log --preset nginx --where "status>=500"
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**Terminal Output:**
|
|
273
|
+
```text
|
|
274
|
+
Parsed Logs
|
|
275
|
+
βββββββββ¬βββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬ββββββββ¬βββββββ¬ββββββββ¬ββββββββ
|
|
276
|
+
βip β user β time β methβ¦ β path β protβ¦ β statβ¦ β size β refeβ¦ β user_β¦β
|
|
277
|
+
βββββββββΌβββββββΌββββββββΌββββββββΌββββββββΌββββββββΌββββββββΌβββββββΌββββββββΌββββββββ€
|
|
278
|
+
β192.1β¦ β - β 10/Oβ¦ β POST β /apiβ¦ β HTTPβ¦ β 500 β 245 β httpβ¦ β Mozilβ¦β
|
|
279
|
+
β β β +0000 β β β β β β β β
|
|
280
|
+
β10.0.β¦ β - β 10/Oβ¦ β GET β /apiβ¦ β HTTPβ¦ β 502 β 0 β - β curl/β¦β
|
|
281
|
+
β β β +0000 β β β β β β β β
|
|
282
|
+
β10.0.β¦ β - β 10/Oβ¦ β POST β /apiβ¦ β HTTPβ¦ β 500 β 412 β - β Mozilβ¦β
|
|
283
|
+
β β β +0000 β β β β β β β β
|
|
284
|
+
β192.1β¦ β - β 10/Oβ¦ β PUT β /apiβ¦ β HTTPβ¦ β 502 β 128 β httpβ¦ β Mozilβ¦β
|
|
285
|
+
β β β +0000 β β β β β β β β
|
|
286
|
+
βββββββββ΄βββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄ββββββββ΄βββββββ΄ββββββββ΄ββββββββ
|
|
287
|
+
Showing 4 of 4 rows
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
### 3. Combining Multiple Filters (AND Logic)
|
|
293
|
+
Combine status code checks with regex pattern searches (`~`) on URL paths:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
loglens sample_logs/nginx.log --preset nginx --where "method=POST" --where "path~/api"
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
### 4. Inspecting Malformed / Unmatched Lines
|
|
302
|
+
When a log file has corrupt lines or non-standard entries, LogLens flags them without crashing. Use `--show-unmatched`:
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
loglens sample_logs/nginx.log --preset nginx --show-unmatched
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
**Terminal Output:**
|
|
309
|
+
```text
|
|
310
|
+
ββββββββββββββββββββββββββ Unmatched Lines (3 total) ββββββββββββββββββββββββββ
|
|
311
|
+
β Line 7: MALFORMED LINE 1: This is total garbage from an interrupted β
|
|
312
|
+
β connection β
|
|
313
|
+
β Line 12: BROKEN LINE 2: [10/Oct/2025:13:56:22 +0000] Missing IP and user β
|
|
314
|
+
β info β
|
|
315
|
+
β Line 18: CORRUPTED LINE 3: 192.168.1.99 - - "BAD_REQUEST_NO_TIMESTAMP" 400 β
|
|
316
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Notice that original file line numbers (`Line 7`, `Line 12`, `Line 18`) are accurately reported!
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
### 5. Statistical Summaries & ASCII Bar Charts
|
|
324
|
+
Compute request totals, top client IPs, status distribution, top endpoints, and errors-per-minute with `--stats`:
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
loglens sample_logs/nginx.log --preset nginx --stats
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
**Terminal Output:**
|
|
331
|
+
```text
|
|
332
|
+
ββ LogLens Statistics Overview ββ
|
|
333
|
+
β Total Non-Empty Lines: 20 β
|
|
334
|
+
β Matched Rows: 17 β
|
|
335
|
+
β Unmatched Lines: 3 β
|
|
336
|
+
β Match Rate: 85.0% β
|
|
337
|
+
βββββββββββββββββββββββββββββββββ
|
|
338
|
+
Top 5 Client IPs
|
|
339
|
+
βββββββββββββββ¬βββββββββββ
|
|
340
|
+
β IP Address β Requests β
|
|
341
|
+
βββββββββββββββΌβββββββββββ€
|
|
342
|
+
β 192.168.1.5 β 4 β
|
|
343
|
+
β 10.0.0.2 β 4 β
|
|
344
|
+
β 192.168.1.6 β 1 β
|
|
345
|
+
β 192.168.1.7 β 1 β
|
|
346
|
+
β 10.0.0.5 β 1 β
|
|
347
|
+
βββββββββββββββ΄βββββββββββ
|
|
348
|
+
HTTP Status Code
|
|
349
|
+
Distribution
|
|
350
|
+
βββββββββββββββ¬ββββββββ
|
|
351
|
+
β Status Code β Count β
|
|
352
|
+
βββββββββββββββΌββββββββ€
|
|
353
|
+
β 200 β 10 β
|
|
354
|
+
β 301 β 1 β
|
|
355
|
+
β 404 β 2 β
|
|
356
|
+
β 500 β 2 β
|
|
357
|
+
β 502 β 2 β
|
|
358
|
+
βββββββββββββββ΄ββββββββ
|
|
359
|
+
Top 5 Requested Paths
|
|
360
|
+
ββββββββββββββββββββ¬ββββββββ
|
|
361
|
+
β Path β Count β
|
|
362
|
+
ββββββββββββββββββββΌββββββββ€
|
|
363
|
+
β /index.html β 2 β
|
|
364
|
+
β /api/inventory β 2 β
|
|
365
|
+
β /api/login β 1 β
|
|
366
|
+
β /old-page β 1 β
|
|
367
|
+
β /images/logo.png β 1 β
|
|
368
|
+
ββββββββββββββββββββ΄ββββββββ
|
|
369
|
+
Errors Per Minute (Status >= 500 or ERROR/CRITICAL/FATAL)
|
|
370
|
+
ββββββββββββββββββββ¬ββββββββ¬βββββββββββββββββββββββββββββββββ
|
|
371
|
+
β Minute Window β Count β Distribution β
|
|
372
|
+
ββββββββββββββββββββΌββββββββΌβββββββββββββββββββββββββββββββββ€
|
|
373
|
+
β 2025-10-10 13:56 β 4 β ββββββββββββββββββββββββββββββ β
|
|
374
|
+
ββββββββββββββββββββ΄ββββββββ΄βββββββββββββββββββββββββββββββββ
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
---
|
|
378
|
+
|
|
379
|
+
### 6. Exporting to CSV and JSON
|
|
380
|
+
Export filtered results directly to CSV or JSON using `--out`:
|
|
381
|
+
|
|
382
|
+
```bash
|
|
383
|
+
# Export all 5xx server errors to CSV
|
|
384
|
+
loglens sample_logs/nginx.log --preset nginx --where "status>=500" --out errors.csv
|
|
385
|
+
|
|
386
|
+
# Export application errors to JSON
|
|
387
|
+
loglens sample_logs/app.log --preset generic --where "level=ERROR" --out errors.json
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
**Content of `errors.json`:**
|
|
391
|
+
```json
|
|
392
|
+
[
|
|
393
|
+
{
|
|
394
|
+
"time": "2025-10-10 13:55:36",
|
|
395
|
+
"level": "ERROR",
|
|
396
|
+
"message": "Database connection timed out"
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
"time": "2025-10-10 13:56:35",
|
|
400
|
+
"level": "ERROR",
|
|
401
|
+
"message": "Payment gateway returned status code 503"
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
"time": "2025-10-10 13:57:35",
|
|
405
|
+
"level": "ERROR",
|
|
406
|
+
"message": "Failed to dispatch webhook to endpoint https://webhook.site/notify"
|
|
407
|
+
}
|
|
408
|
+
]
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
### 7. Streaming Logs via Stdin (UNIX Pipe / PowerShell)
|
|
414
|
+
LogLens seamlessly accepts standard input when piping from other commands:
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
# UNIX / Linux / macOS:
|
|
418
|
+
cat sample_logs/syslog.log | loglens --preset syslog --limit 5
|
|
419
|
+
|
|
420
|
+
# Windows PowerShell:
|
|
421
|
+
Get-Content sample_logs/syslog.log | loglens --preset syslog --limit 5
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
**Terminal Output:**
|
|
425
|
+
```text
|
|
426
|
+
Parsed Logs
|
|
427
|
+
ββββββββββββββββββ¬βββββββββ¬ββββββββββ¬βββββββ¬βββββββββββββββββββββββββββββββββββ
|
|
428
|
+
βtime β host β process β pid β message β
|
|
429
|
+
ββββββββββββββββββΌβββββββββΌββββββββββΌβββββββΌβββββββββββββββββββββββββββββββββββ€
|
|
430
|
+
βOct 10 13:55:36 β myhost β sshd β 1234 β Failed password for root from β
|
|
431
|
+
β β β β β 10.0.0.... β
|
|
432
|
+
βOct 10 13:55:38 β myhost β sshd β 1234 β Failed password for root from β
|
|
433
|
+
β β β β β 10.0.0.... β
|
|
434
|
+
βOct 10 13:55:40 β myhost β sshd β 1234 β Accepted publickey for ubuntu β
|
|
435
|
+
β β β β β from 19... β
|
|
436
|
+
βOct 10 13:55:45 β myhost β systemd β 1 β Starting Daily apt upgrade and β
|
|
437
|
+
β β β β β clean ... β
|
|
438
|
+
βOct 10 13:55:50 β myhost β cron β 567 β (root) CMD β
|
|
439
|
+
β β β β β (/usr/local/bin/backup.sh ... β
|
|
440
|
+
ββββββββββββββββββ΄βββββββββ΄ββββββββββ΄βββββββ΄βββββββββββββββββββββββββββββββββββ
|
|
441
|
+
Showing 5 of 15 rows
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
### 8. Custom Regex with Named Capture Groups
|
|
447
|
+
Have a custom proprietary log format? Simply pass your regex with named capture groups using `--regex`:
|
|
448
|
+
|
|
449
|
+
```bash
|
|
450
|
+
loglens sample_logs/app.log --regex "^(?P<timestamp>\S+ \S+) (?P<severity>\w+) (?P<details>.*)$"
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
If you forget to provide named groups, LogLens guides you with an immediate friendly error:
|
|
454
|
+
```text
|
|
455
|
+
Error: Regex must contain at least one named group, e.g. (?P<ip>\S+)
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
---
|
|
459
|
+
|
|
460
|
+
### 9. Automatic Regex Suggestion from a Sample Line
|
|
461
|
+
Don't want to write regex from scratch? Pass a sample line to `--suggest`:
|
|
462
|
+
|
|
463
|
+
```bash
|
|
464
|
+
loglens --suggest '192.168.1.5 - - [10/Oct/2025:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 1024'
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
**Terminal Output:**
|
|
468
|
+
```text
|
|
469
|
+
ββββββββββββββββββββββββ Suggested Regular Expression βββββββββββββββββββββββββ
|
|
470
|
+
β ^(?P<ip>\d{1,3}(?:\.\d{1,3}){3})\s+\-\s+\-\s+\[(?P<time>[^\]]+)\]\s+"(?P<me β
|
|
471
|
+
β thod>[A-Z]+)\s+(?P<path>\S+)\s+HTTP/(?P<num1>\d+)\.(?P<num2>\d+)"\s+(?P<sta β
|
|
472
|
+
β tus>\d{3})\s+(?P<num3>\d+)$ β
|
|
473
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
474
|
+
Extracted Named Groups from Sample
|
|
475
|
+
ββββββββββββββ¬βββββββββββββββββββββββββββββ
|
|
476
|
+
β Group Name β Extracted Value β
|
|
477
|
+
ββββββββββββββΌβββββββββββββββββββββββββββββ€
|
|
478
|
+
β ip β 192.168.1.5 β
|
|
479
|
+
β time β 10/Oct/2025:13:55:36 +0000 β
|
|
480
|
+
β method β GET β
|
|
481
|
+
β path β /index.html β
|
|
482
|
+
β num1 β 1 β
|
|
483
|
+
β num2 β 1 β
|
|
484
|
+
β status β 200 β
|
|
485
|
+
β num3 β 1024 β
|
|
486
|
+
ββββββββββββββ΄βββββββββββββββββββββββββββββ
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
---
|
|
490
|
+
|
|
491
|
+
## Module Reference
|
|
492
|
+
|
|
493
|
+
The code is organized into modular single-responsibility units:
|
|
494
|
+
|
|
495
|
+
| File | Purpose | Key Functions / Classes |
|
|
496
|
+
|---|---|---|
|
|
497
|
+
| `loglens/__init__.py` | Package version declaration (`__version__ = "0.1.0"`) | `__version__` |
|
|
498
|
+
| `loglens/__main__.py` | Enables execution via `python -m loglens` | `main()` |
|
|
499
|
+
| `loglens/presets.py` | Pre-compiled regex patterns for standard logs | `PRESETS` dict |
|
|
500
|
+
| `loglens/parser.py` | Memory-efficient streaming generator | `compile_user_regex()`, `parse_lines()` |
|
|
501
|
+
| `loglens/filters.py` | Filtering logic with numerical and regex support | `parse_where()`, `apply_filters()` |
|
|
502
|
+
| `loglens/stats.py` | Aggregates counts, top entities, and errors/min | `compute_stats()` |
|
|
503
|
+
| `loglens/export.py` | Serializes records to `.csv` or `.json` | `export_rows()` |
|
|
504
|
+
| `loglens/suggest.py` | Rule-based regex tokenizer and generator | `suggest_regex()` |
|
|
505
|
+
| `loglens/display.py` | Terminal styling and Rich components | `show_table()`, `show_unmatched()`, `show_stats()`, `show_suggestion()` |
|
|
506
|
+
| `loglens/cli.py` | CLI argument parsing, execution flow & error handling | `main()`, `run()` |
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
## Running Tests
|
|
511
|
+
|
|
512
|
+
All modules include unit tests with 100% core test coverage. To execute the test suite:
|
|
513
|
+
|
|
514
|
+
```bash
|
|
515
|
+
pytest -v
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
**Test Suite Summary:**
|
|
519
|
+
```text
|
|
520
|
+
collected 34 items
|
|
521
|
+
|
|
522
|
+
tests/test_cli.py::test_cli_suggest_exits_zero PASSED [ 2%]
|
|
523
|
+
tests/test_cli.py::test_cli_missing_both_preset_and_regex PASSED [ 5%]
|
|
524
|
+
tests/test_cli.py::test_cli_both_preset_and_regex PASSED [ 8%]
|
|
525
|
+
tests/test_cli.py::test_cli_missing_file_error PASSED [ 11%]
|
|
526
|
+
tests/test_cli.py::test_cli_parse_nginx_table PASSED [ 14%]
|
|
527
|
+
tests/test_cli.py::test_cli_parse_nginx_where_filter PASSED [ 17%]
|
|
528
|
+
tests/test_cli.py::test_cli_parse_nginx_unmatched PASSED [ 20%]
|
|
529
|
+
tests/test_cli.py::test_cli_parse_nginx_stats PASSED [ 23%]
|
|
530
|
+
tests/test_cli.py::test_cli_export_csv_and_json PASSED [ 26%]
|
|
531
|
+
tests/test_cli.py::test_cli_stdin_reading PASSED [ 29%]
|
|
532
|
+
tests/test_export.py::test_export_csv_round_trip PASSED [ 32%]
|
|
533
|
+
tests/test_export.py::test_export_json_round_trip PASSED [ 35%]
|
|
534
|
+
tests/test_export.py::test_export_empty_results PASSED [ 38%]
|
|
535
|
+
tests/test_export.py::test_unsupported_extension_raises_value_error PASSED [ 41%]
|
|
536
|
+
tests/test_filters.py::test_parse_where_valid PASSED [ 44%]
|
|
537
|
+
tests/test_filters.py::test_parse_where_invalid_syntax PASSED [ 47%]
|
|
538
|
+
tests/test_filters.py::test_numeric_comparison_greater_than PASSED [ 50%]
|
|
539
|
+
tests/test_filters.py::test_case_insensitive_string_equality PASSED [ 52%]
|
|
540
|
+
tests/test_filters.py::test_regex_search_operator PASSED [ 55%]
|
|
541
|
+
tests/test_filters.py::test_multiple_filters_and_logic PASSED [ 58%]
|
|
542
|
+
tests/test_filters.py::test_unknown_field_raises_value_error PASSED [ 61%]
|
|
543
|
+
tests/test_filters.py::test_invalid_regex_in_filter_raises_value_error PASSED [ 64%]
|
|
544
|
+
tests/test_parser.py::test_valid_nginx_line_parsed_correctly PASSED [ 67%]
|
|
545
|
+
tests/test_parser.py::test_malformed_line_yields_none PASSED [ 70%]
|
|
546
|
+
tests/test_parser.py::test_blank_lines_are_skipped PASSED [ 73%]
|
|
547
|
+
tests/test_parser.py::test_regex_with_no_named_groups_raises_value_error PASSED [ 76%]
|
|
548
|
+
tests/test_parser.py::test_invalid_regex_syntax_raises_value_error PASSED [ 79%]
|
|
549
|
+
tests/test_parser.py::test_compile_user_regex_success PASSED [ 82%]
|
|
550
|
+
tests/test_stats.py::test_compute_stats_counts_and_metrics PASSED [ 85%]
|
|
551
|
+
tests/test_stats.py::test_unparseable_time_does_not_crash_errors_per_minute PASSED [ 88%]
|
|
552
|
+
tests/test_stats.py::test_syslog_timestamp_errors_per_minute PASSED [ 91%]
|
|
553
|
+
tests/test_stats.py::test_empty_rows PASSED [ 94%]
|
|
554
|
+
tests/test_suggest.py::test_suggest_regex_matches_sample_lines_from_spec PASSED [ 97%]
|
|
555
|
+
tests/test_suggest.py::test_suggest_fallback_on_unusual_input PASSED [100%]
|
|
556
|
+
|
|
557
|
+
============================= 34 passed in 0.49s ==============================
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## Dedicated Regex Guide
|
|
563
|
+
|
|
564
|
+
For an in-depth reference written specifically for developers who already understand regex and want to write complex filters or extract nested patterns, see [REGEX_GUIDE.md](REGEX_GUIDE.md).
|
|
565
|
+
|
|
566
|
+
---
|
|
567
|
+
|
|
568
|
+
## License
|
|
569
|
+
|
|
570
|
+
MIT License. See [LICENSE](LICENSE) for details.
|