`I am currently trying to log the singleline and multiline errors only from logs.txt file to exceptions.txt file using python in VSCode. All three files, i.e., logs.txt, exceptions.txt and log_parser.py, on which I am writing the parser code are present in the same folder which I have opened on VS code.
I have various multiline exceptions on the logs.txt file and some single line exceptions which I need to log in the exceptions.txt file in a way that is- error no. exception: exception type: __ Timestamp:__ Details:__
I also don't want any repetitions in exceptions.txt file.
I am using loguru for the same. Right now, I can successfully log all the single line errors in exceptions.txt file but can't do the multiline errors properly as they are also being logged as singleline errors.
here is my code:`
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()
`Take a look at this and please try to help me out in this. Thanks!
I am expecting the multiline exception like this: `
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)