logsquash 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,34 @@
1
+ Metadata-Version: 2.4
2
+ Name: logsquash
3
+ Version: 0.1.0
4
+ Summary: Collapse repeated log lines (ignoring timestamps, numbers, ids) into counted groups. Stdlib only.
5
+ Author-email: agent-one <rafal.baranowski.iot@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/rafal-one/logsquash
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Requires-Python: >=3.9
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Dynamic: license-file
14
+
15
+ # logsquash
16
+
17
+ Collapse repeated log lines into counted groups, ignoring the parts that change:
18
+ timestamps, IPs, UUIDs, hex ids and numbers. Like `sort | uniq -c`, but "conn 10.0.0.1 failed
19
+ after 30ms" and "conn 10.0.0.9 failed after 45ms" count as the same message. Python 3.9+, stdlib only.
20
+
21
+ ```
22
+ pip install logsquash
23
+ logsquash app.log -s # groups by frequency, first example line shown
24
+ logsquash app.log -s -m 5 -t # only groups seen >= 5 times, show the normalized template
25
+ kubectl logs pod | logsquash -a # only merge consecutive repeats
26
+ ```
27
+
28
+ Output: `COUNT line`. Exit codes: 0 ok, 2 file error.
29
+
30
+ ## Tests
31
+ `pytest` (8 tests).
32
+
33
+ ## Support
34
+ Free, MIT licensed. If it saved you time: https://paypal.me/RafalBaranowski
@@ -0,0 +1,7 @@
1
+ logsquash.py,sha256=rF7wgrgBEPpUzlri_MK1zhbyQhkgEnpNd6WVms_EDGk,2945
2
+ logsquash-0.1.0.dist-info/licenses/LICENSE,sha256=-hZriFiksFsGGM2WbhdnQzoXUOFuMGve5P3y4pvA0Ws,1066
3
+ logsquash-0.1.0.dist-info/METADATA,sha256=z-Hgx0V_Z6QBNL9ysVj76tyvQncJdpir3mgcXP3-CfM,1250
4
+ logsquash-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
5
+ logsquash-0.1.0.dist-info/entry_points.txt,sha256=ZaFBVa1w-W5hJ-eOzd8J8cC-LPWWbrG9-X4HZlplNRc,45
6
+ logsquash-0.1.0.dist-info/top_level.txt,sha256=bnK0QprWTyNTdHXz-2_HdcPVCP1S__2Zxb1CtBvZ4hM,10
7
+ logsquash-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ logsquash = logsquash:main
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 agent-one
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ logsquash
logsquash.py ADDED
@@ -0,0 +1,84 @@
1
+ """logsquash: collapse near-duplicate log lines into counted groups."""
2
+ import argparse
3
+ import re
4
+ import sys
5
+
6
+ __version__ = "0.1.0"
7
+
8
+ _PATTERNS = [
9
+ (re.compile(r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?"), "<TS>"),
10
+ (re.compile(r"\b\d{2}:\d{2}:\d{2}(?:[.,]\d+)?\b"), "<TIME>"),
11
+ (re.compile(r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b"), "<UUID>"),
12
+ (re.compile(r"\b\d{1,3}(?:\.\d{1,3}){3}\b"), "<IP>"),
13
+ (re.compile(r"\b0x[0-9a-fA-F]+\b"), "<HEX>"),
14
+ (re.compile(r"\b[0-9a-fA-F]{12,}\b"), "<HEX>"),
15
+ (re.compile(r"\d+(?:\.\d+)?"), "<N>"),
16
+ ]
17
+
18
+
19
+ def normalize(line):
20
+ """Replace volatile tokens (timestamps, ids, numbers) by placeholders."""
21
+ for rx, repl in _PATTERNS:
22
+ line = rx.sub(repl, line)
23
+ return line.strip()
24
+
25
+
26
+ def squash(lines, adjacent=False):
27
+ """Return list of [count, first_line, last_line, key] in first-seen order.
28
+
29
+ adjacent=True only merges consecutive runs (like uniq); otherwise merges globally.
30
+ """
31
+ groups = []
32
+ index = {}
33
+ for line in lines:
34
+ line = line.rstrip("\r\n")
35
+ if not line.strip():
36
+ continue
37
+ key = normalize(line)
38
+ if adjacent:
39
+ if groups and groups[-1][3] == key:
40
+ groups[-1][0] += 1
41
+ groups[-1][2] = line
42
+ else:
43
+ groups.append([1, line, line, key])
44
+ elif key in index:
45
+ g = index[key]
46
+ g[0] += 1
47
+ g[2] = line
48
+ else:
49
+ g = [1, line, line, key]
50
+ index[key] = g
51
+ groups.append(g)
52
+ return groups
53
+
54
+
55
+ def main(argv=None):
56
+ p = argparse.ArgumentParser(prog="logsquash", description=__doc__)
57
+ p.add_argument("file", nargs="?", help="log file (default: stdin)")
58
+ p.add_argument("-a", "--adjacent", action="store_true", help="only merge consecutive lines")
59
+ p.add_argument("-s", "--sort", action="store_true", help="sort by count, descending")
60
+ p.add_argument("-m", "--min-count", type=int, default=1, help="hide groups seen fewer times")
61
+ p.add_argument("-t", "--template", action="store_true", help="show normalized template, not the first line")
62
+ p.add_argument("--version", action="version", version=__version__)
63
+ args = p.parse_args(argv)
64
+ try:
65
+ if args.file:
66
+ with open(args.file, encoding="utf-8", errors="replace") as fh:
67
+ lines = fh.readlines()
68
+ else:
69
+ lines = sys.stdin.read().splitlines()
70
+ except OSError as e:
71
+ print(f"logsquash: {e}", file=sys.stderr)
72
+ return 2
73
+ groups = squash(lines, adjacent=args.adjacent)
74
+ if args.sort:
75
+ groups.sort(key=lambda g: -g[0])
76
+ for count, first, _last, key in groups:
77
+ if count < args.min_count:
78
+ continue
79
+ print(f"{count:>7} {key if args.template else first}")
80
+ return 0
81
+
82
+
83
+ if __name__ == "__main__":
84
+ sys.exit(main())