logsquash 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.
@@ -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,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,20 @@
1
+ # logsquash
2
+
3
+ Collapse repeated log lines into counted groups, ignoring the parts that change:
4
+ timestamps, IPs, UUIDs, hex ids and numbers. Like `sort | uniq -c`, but "conn 10.0.0.1 failed
5
+ after 30ms" and "conn 10.0.0.9 failed after 45ms" count as the same message. Python 3.9+, stdlib only.
6
+
7
+ ```
8
+ pip install logsquash
9
+ logsquash app.log -s # groups by frequency, first example line shown
10
+ logsquash app.log -s -m 5 -t # only groups seen >= 5 times, show the normalized template
11
+ kubectl logs pod | logsquash -a # only merge consecutive repeats
12
+ ```
13
+
14
+ Output: `COUNT line`. Exit codes: 0 ok, 2 file error.
15
+
16
+ ## Tests
17
+ `pytest` (8 tests).
18
+
19
+ ## Support
20
+ Free, MIT licensed. If it saved you time: https://paypal.me/RafalBaranowski
@@ -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,10 @@
1
+ LICENSE
2
+ README.md
3
+ logsquash.py
4
+ pyproject.toml
5
+ logsquash.egg-info/PKG-INFO
6
+ logsquash.egg-info/SOURCES.txt
7
+ logsquash.egg-info/dependency_links.txt
8
+ logsquash.egg-info/entry_points.txt
9
+ logsquash.egg-info/top_level.txt
10
+ tests/test_logsquash.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ logsquash = logsquash:main
@@ -0,0 +1 @@
1
+ logsquash
@@ -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())
@@ -0,0 +1,25 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "logsquash"
7
+ version = "0.1.0"
8
+ description = "Collapse repeated log lines (ignoring timestamps, numbers, ids) into counted groups. Stdlib only."
9
+ readme = "README.md"
10
+ license = {text = "MIT"}
11
+ requires-python = ">=3.9"
12
+ authors = [{name = "agent-one", email = "rafal.baranowski.iot@gmail.com"}]
13
+ classifiers = ["License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3"]
14
+
15
+ [project.urls]
16
+ Homepage = "https://github.com/rafal-one/logsquash"
17
+
18
+ [project.scripts]
19
+ logsquash = "logsquash:main"
20
+
21
+ [tool.setuptools]
22
+ py-modules = ["logsquash"]
23
+
24
+ [tool.pytest.ini_options]
25
+ pythonpath = ["."]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,55 @@
1
+ import subprocess
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ import logsquash as ls
6
+
7
+ LOG = """2026-01-01 10:00:01 ERROR conn 10.0.0.1 failed after 30ms
8
+ 2026-01-01 10:00:02 INFO ok
9
+ 2026-01-01 10:00:03 ERROR conn 10.0.0.9 failed after 45ms
10
+ """
11
+
12
+
13
+ def test_normalize_ts_ip_num():
14
+ assert ls.normalize("2026-01-01T10:00:01Z a 10.0.0.1 took 3.5s") == "<TS> a <IP> took <N>s"
15
+
16
+
17
+ def test_normalize_uuid_hex():
18
+ assert ls.normalize("id 123e4567-e89b-12d3-a456-426614174000 0xdeadbeef") == "id <UUID> <HEX>"
19
+
20
+
21
+ def test_global_merge():
22
+ g = ls.squash(LOG.splitlines())
23
+ assert [x[0] for x in g] == [2, 1]
24
+ assert g[0][1].startswith("2026-01-01 10:00:01")
25
+ assert g[0][2].startswith("2026-01-01 10:00:03")
26
+
27
+
28
+ def test_adjacent_only():
29
+ g = ls.squash(LOG.splitlines(), adjacent=True)
30
+ assert [x[0] for x in g] == [1, 1, 1]
31
+
32
+
33
+ def test_blank_and_empty():
34
+ assert ls.squash([]) == []
35
+ assert ls.squash(["", " ", "\n"]) == []
36
+
37
+
38
+ def test_cli_sort_min(tmp_path, capsys):
39
+ f = tmp_path / "a.log"
40
+ f.write_text(LOG)
41
+ assert ls.main([str(f), "-s", "-m", "2", "-t"]) == 0
42
+ out = capsys.readouterr().out
43
+ assert out.strip() == "2 <TS> ERROR conn <IP> failed after <N>ms"
44
+
45
+
46
+ def test_cli_missing_file(capsys):
47
+ assert ls.main(["/nonexistent/x.log"]) == 2
48
+ assert "logsquash:" in capsys.readouterr().err
49
+
50
+
51
+ def test_cli_stdin():
52
+ r = subprocess.run([sys.executable, str(Path(__file__).parent.parent / "logsquash.py")],
53
+ input="a 1\na 2\nb\n", capture_output=True, text=True)
54
+ assert r.returncode == 0
55
+ assert r.stdout.splitlines()[0].strip().startswith("2")