From bbd6c67f68633d065032370443801861846d1d4d Mon Sep 17 00:00:00 2001 From: octodog Date: Thu, 9 Jan 2025 16:43:51 +0900 Subject: [PATCH] fix(BA-477): Logging and formatting exceptions raised from local process (#3410) (#3414) Co-authored-by: Joongi Kim --- changes/3410.fix.md | 1 + src/ai/backend/logging/formatter.py | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 changes/3410.fix.md diff --git a/changes/3410.fix.md b/changes/3410.fix.md new file mode 100644 index 0000000000..e4dcf17c68 --- /dev/null +++ b/changes/3410.fix.md @@ -0,0 +1 @@ +Fix formatting errors when logging exceptions raised from the current local process that did not pass our custom serialization step diff --git a/src/ai/backend/logging/formatter.py b/src/ai/backend/logging/formatter.py index 383fd9987f..3b180cc9ad 100644 --- a/src/ai/backend/logging/formatter.py +++ b/src/ai/backend/logging/formatter.py @@ -3,17 +3,31 @@ import logging import pprint import time +import traceback +from collections.abc import Sequence from datetime import datetime -from typing import Any +from types import TracebackType +from typing import Any, TypeAlias, cast import coloredlogs from pythonjsonlogger.json import JsonFormatter - -def format_exception(self, ei) -> str: - s = "".join(ei) - if s[-1:] == "\n": - s = s[:-1] +_SysExcInfoType: TypeAlias = ( + tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] +) + + +def format_exception(self, ei: Sequence[str] | _SysExcInfoType) -> str: + match ei: + case (str(), *_): + # Already foramtted from the source process for ease of serialization + s = "".join(cast(Sequence[str], ei)) # cast is required for mypy + case (type(), BaseException(), _): + # A live exc_info object from the current process + s = "".join(traceback.format_exception(*ei)) + case _: + s = "" + s = s.rstrip("\n") return s