#!/usr/bin/env python3 """Фильтрует JSON Lines логи.""" from __future__ import annotations import argparse import json import sys from collections import Counter from pathlib import Path def nested(record: dict, dotted: str): current = record for part in dotted.split("."): if not isinstance(current, dict) or part not in current: return None current = current[part] return current def matches(record: dict, args) -> bool: level = str(record.get("level", record.get("severity", ""))).lower() if args.level: allowed = {value.strip().lower() for value in args.level.split(",") if value.strip()} if level not in allowed: return False if args.field: value = nested(record, args.field) if args.value is not None and str(value) != args.value: return False if args.value is None and value is None: return False if args.contains: text = json.dumps(record, ensure_ascii=False).lower() if args.contains.lower() not in text: return False return True def main() -> int: parser = argparse.ArgumentParser(description="Фильтр JSON-логов") parser.add_argument("--file", required=True, type=Path) parser.add_argument("--level", default="") parser.add_argument("--field", default="") parser.add_argument("--value", default=None) parser.add_argument("--contains", default="") parser.add_argument("--limit", type=int, default=20) parser.add_argument("--json", action="store_true") args = parser.parse_args() try: lines = args.file.read_text(encoding="utf-8", errors="replace").splitlines() except OSError as error: print(f"ОШИБКА: {error}", file=sys.stderr) return 2 selected = [] levels = Counter() invalid = 0 for line in lines: if not line.strip(): continue try: record = json.loads(line) except json.JSONDecodeError: invalid += 1 continue if not isinstance(record, dict): invalid += 1 continue levels[str(record.get("level", record.get("severity", "unknown"))).lower()] += 1 if matches(record, args): selected.append(record) shown = selected[: max(args.limit, 0)] if args.json: print(json.dumps({"total": len(selected), "records": shown, "levels": levels}, ensure_ascii=False, indent=2)) else: print(f"Строк: {len(lines)}, битых: {invalid}, выбрано: {len(selected)}") print("Уровни: " + ", ".join(f"{key}={value}" for key, value in levels.items())) for record in shown: print(json.dumps(record, ensure_ascii=False)) if len(selected) > len(shown): print(f"Показано {len(shown)} из {len(selected)}") return 0 if __name__ == "__main__": raise SystemExit(main())