提问人:anything 提问时间:6/22/2023 更新时间:6/22/2023 访问量:18
记录从logs.txt到exceptions.txt的多行错误
Log multiline errors from logs.txt to exceptions.txt
问:
'我目前正在尝试使用 VSCode 中的 python 仅记录logs.txt文件到exceptions.txt文件的单行和多行错误。我正在编写解析器代码的所有三个文件(即 logs.txt、exceptions.txt 和 log_parser.py)都存在于我在 VS Code 上打开的同一文件夹中。
我在 logs.txt 文件上有各种多行异常,还有一些单行异常,我需要以一种方式登录exceptions.txt文件—— 错误编号。例外: 异常类型:__ 时间戳:__ 详:__
我也不希望exceptions.txt文件中有任何重复。
我正在使用 loguru 做同样的事情。 现在,我可以成功地将所有单行错误记录在exceptions.txt文件中,但无法正确执行多行错误,因为它们也被记录为单行错误。
这是我的代码:'
from loguru import logger
import re
logger.add("exceptions.txt", format="{message}", level="ERROR")
def log_exceptions():
with open("logs.txt", "r", encoding="utf-8") as file:
logs = file.read()
pattern = r"\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}.*? E (.*?): (.*?)(?=\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}.*? E .*?:|$)"
errors = re.finditer(pattern, logs, flags=re.DOTALL)
error_set = set()
error_number = 1
multiline_exception = False
multiline_exception_type = ""
multiline_exception_details = ""
for match in errors:
exception_line = match.group(1).strip()
exception_details = match.group(2).strip()
if multiline_exception:
if exception_line.startswith("java.lang.") or exception_line.startswith("at "):
multiline_exception_details += f"\n{exception_line}"
else:
logger.error(f"{error_number}. {multiline_exception_type}")
logger.error(f"Exception Type: {multiline_exception_type}")
logger.error(f"Details:\n{multiline_exception_details}")
error_set.add(multiline_exception_details)
error_number += 1
multiline_exception = False
exception_type_match = re.search(r"java\.lang\.(.*?)\b", exception_line)
if exception_type_match:
exception_type = exception_type_match.group(1)
multiline_exception = True
multiline_exception_type = exception_type
multiline_exception_details = exception_details
else:
exception_type = exception_line.split(".")[-1]
if exception_details not in error_set:
logger.error(f"{error_number}. {exception_type}")
logger.error(f"Exception Type: {exception_type}")
logger.error(f"Details:\n{exception_details}")
error_set.add(exception_details)
error_number += 1
# Additional handling for the last multi-line exception in the logs
if multiline_exception:
logger.error(f"{error_number}. {multiline_exception_type}")
logger.error(f"Exception Type: {multiline_exception_type}")
logger.error(f"Details:\n{multiline_exception_details}")
# Usage
log_exceptions()
“看看这个,请试着帮助我。谢谢!
我期待这样的多行异常: `
06-21 17:54:47.663 511 546 E ActivityManager: Failure starting process com.google.as
06-21 17:54:47.663 511 546 E ActivityManager: java.lang.SecurityException: Package com.google.as is currently frozen!
06-21 17:54:47.663 511 546 E ActivityManager: at a.b.c(d:8987)
06-21 17:54:47.663 511 546 E ActivityManager: at d.e.f(k:7364)
06-21 17:54:47.663 511 546 E ActivityManager: at p.k.h(i:9876)
06-21 17:54:47.663 511 546 E ActivityManager: at k.s.u(g:986)
06-21 17:54:47.663 511 546 E ActivityManager: at p.o.u(y:734)
06-21 17:54:47.663 511 546 E ActivityManager: at f.o.t(s:3937)
06-21 17:54:47.663 511 546 E ActivityManager: at d.c.y(k:2732)
06-21 17:54:47.663 511 546 E ActivityManager: at r.d.h(l:3474)
06-21 17:54:47.663 511 546 E ActivityManager: at i.p.s(u:09283)
06-21 17:54:47.663 511 546 E ActivityManager: at d.m.r(c:9873)
06-21 17:54:47.663 511 546 E ActivityManager: at a.d.b(i:367)
06-21 17:54:47.663 511 546 E ActivityManager: at b.d.o(o:0974)
06-21 17:54:47.663 511 546 E ActivityManager: at r.b.o(t:8546)
06-21 17:54:47.663 511 546 E ActivityManager: at g.k.o(w:9287)
06-21 17:54:47.663 511 546 E ActivityManager: at o.a.p(k:387)
06-21 17:54:47.663 511 546 E ActivityManager: at h.d.j(o:38374)
06-21 17:54:47.663 511 546 E ActivityManager: at k.f.d(p:3448)
06-21 17:54:47.663 511 546 E ActivityManager: at j.l.w(x:9844)
06-21 17:54:47.663 511 546 E ActivityManager: at p.s.k(p:2383)
06-21 17:54:47.663 511 546 E ActivityManager: at f.k.w(u:8373)
to be logged like this:
serial number i.e. 1,2,3..). Exception:ActivityManager: Failure starting process com.google.as Exception Type: SecurityException
Timestamp: 06-21 17:54:47.663
Details:
Failure starting process com.google.as
java.lang.SecurityException: Package com.google.as is currently frozen!
at a.b.c(d:8987)
at d.e.f(k:7364)
at p.k.h(i:9876)
at k.s.u(g:986)
at p.o.u(y:734)
at f.o.t(s:3937)
at d.c.y(k:2732)
at r.d.h(l:3474)
at i.p.s(u:09283)
at d.m.r(c:9873)
at a.d.b(i:367)
at b.d.o(o:0974)
at r.b.o(t:8546)
at g.k.o(w:9287)
at o.a.p(k:387)
at h.d.j(o:38374)
at k.f.d(p:3448)
at j.l.w(x:9844)
at p.s.k(p:2383)
at f.k.w(u:8373)
答: 暂无答案
评论