#!/usr/bin/env python3 # -*- coding: utf-8 -*- """从容器日志里把每个 500 异常映射到接口 + 首个业务栈帧""" import re, sys, collections path = sys.argv[1] cur_url = "" records = [] buf = None with open(path, encoding="utf-8", errors="replace") as f: for line in f: line = re.sub(r"\x1b\[[0-9;]*m", "", line.rstrip("\n")) m = re.search(r"\[preHandle\]\[开始请求 URL\(([^)]+)\)", line) if m: cur_url = m.group(1) continue if "defaultExceptionHandler" in line: buf = {"url": cur_url, "exc": "", "frames": []} records.append(buf) continue if buf is not None: if not buf["exc"] and re.match(r"^[a-zA-Z].*(Exception|Error)", line): buf["exc"] = line.strip()[:200] elif line.startswith("\tat cn.iocoder") and len(buf["frames"]) < 3: buf["frames"].append(line.strip()[:180]) if line.startswith("\tat ") and len(buf["frames"]) >= 3: buf = None agg = collections.OrderedDict() for r in records: key = r["url"].split("?")[0] agg.setdefault(key, {"exc": r["exc"], "frame": (r["frames"][0] if r["frames"] else ""), "n": 0}) agg[key]["n"] += 1 for url, v in agg.items(): print("%s\n EXC: %s\n FRAME: %s (x%d)" % (url, v["exc"], v["frame"], v["n"])) print("URLS", len(agg))