#!/usr/bin/env python3 # -*- coding: utf-8 -*- """对 DataIntegrityViolationException 抓取具体 SQL 与约束名""" import re, subprocess, sys text = subprocess.run(["docker", "logs", "--since", "15m", "mom-kyzn"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=180).stdout.decode("utf-8", "replace") text = re.sub(r"\x1b\[[0-9;]*m", "", text) lines = text.split("\n") i = 0 cur = "" out = [] while i < len(lines): m = re.search(r"\[preHandle\]\[开始请求 URL\(([^)]+)\)", lines[i]) if m: cur = m.group(1).split("?")[0] if "DataIntegrityViolationException" in lines[i] or "SQLIntegrityConstraintViolationException" in lines[i] or "doesn't have a default value" in lines[i] or "cannot be null" in lines[i]: ctx = [] for l in lines[max(0, i-3): i+6]: if "INSERT INTO" in l or "doesn't have a default value" in l or "cannot be null" in l or "Data too long" in l or "Duplicate entry" in l or "Field " in l: ctx.append(l.strip()[:300]) if ctx: out.append((cur, ctx)) i += 1 seen = set() for cur, ctx in out: key = cur if key in seen: continue seen.add(key) print("###", cur) for c in ctx[:3]: print(" ", c) print("TOTAL", len(out), "UNIQUE", len(seen))