|
|
@@ -5,7 +5,8 @@ import re
|
|
|
import sys
|
|
|
import traceback
|
|
|
from datetime import datetime
|
|
|
-from typing import Literal, Mapping
|
|
|
+from types import TracebackType
|
|
|
+from typing import Any, Literal, Mapping
|
|
|
|
|
|
from termcolor import colored
|
|
|
|
|
|
@@ -61,7 +62,8 @@ class NoColorFormatter(logging.Formatter):
|
|
|
|
|
|
|
|
|
def strip_ansi(s: str) -> str:
|
|
|
- """
|
|
|
+ """Remove ANSI escape sequences (terminal color/formatting codes) from string.
|
|
|
+
|
|
|
Removes ANSI escape sequences from str, as defined by ECMA-048 in
|
|
|
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
|
|
|
# https://github.com/ewen-lbh/python-strip-ansi/blob/master/strip_ansi/__init__.py
|
|
|
@@ -136,6 +138,7 @@ class RollingLogger:
|
|
|
|
|
|
def print_lines(self):
|
|
|
"""Display the last n log_lines in the console (not for file logging).
|
|
|
+
|
|
|
This will create the effect of a rolling display in the console.
|
|
|
"""
|
|
|
self.move_back()
|
|
|
@@ -143,18 +146,14 @@ class RollingLogger:
|
|
|
self.replace_current_line(line)
|
|
|
|
|
|
def move_back(self, amount=-1):
|
|
|
- """
|
|
|
- '\033[F' moves the cursor up one line.
|
|
|
- """
|
|
|
+ r"""'\033[F' moves the cursor up one line."""
|
|
|
if amount == -1:
|
|
|
amount = self.max_lines
|
|
|
self._write('\033[F' * (self.max_lines))
|
|
|
self._flush()
|
|
|
|
|
|
def replace_current_line(self, line=''):
|
|
|
- """
|
|
|
- '\033[2K\r' clears the line and moves the cursor to the beginning of the line.
|
|
|
- """
|
|
|
+ r"""'\033[2K\r' clears the line and moves the cursor to the beginning of the line."""
|
|
|
self._write('\033[2K' + line + '\n')
|
|
|
self._flush()
|
|
|
|
|
|
@@ -232,18 +231,21 @@ def get_file_handler(log_dir: str, log_level: int = logging.INFO):
|
|
|
logging.basicConfig(level=logging.ERROR)
|
|
|
|
|
|
|
|
|
-def log_uncaught_exceptions(ex_cls, ex, tb):
|
|
|
+def log_uncaught_exceptions(
|
|
|
+ ex_cls: type[BaseException], ex: BaseException, tb: TracebackType | None
|
|
|
+) -> Any:
|
|
|
"""Logs uncaught exceptions along with the traceback.
|
|
|
|
|
|
Args:
|
|
|
- ex_cls (type): The type of the exception.
|
|
|
- ex (Exception): The exception instance.
|
|
|
- tb (traceback): The traceback object.
|
|
|
+ ex_cls: The type of the exception.
|
|
|
+ ex: The exception instance.
|
|
|
+ tb: The traceback object.
|
|
|
|
|
|
Returns:
|
|
|
None
|
|
|
"""
|
|
|
- logging.error(''.join(traceback.format_tb(tb)))
|
|
|
+ if tb: # Add check since tb can be None
|
|
|
+ logging.error(''.join(traceback.format_tb(tb)))
|
|
|
logging.error('{0}: {1}'.format(ex_cls, ex))
|
|
|
|
|
|
|
|
|
@@ -283,7 +285,7 @@ logging.getLogger('LiteLLM Proxy').disabled = True
|
|
|
|
|
|
|
|
|
class LlmFileHandler(logging.FileHandler):
|
|
|
- """# LLM prompt and response logging"""
|
|
|
+ """LLM prompt and response logging."""
|
|
|
|
|
|
def __init__(self, filename, mode='a', encoding='utf-8', delay=False):
|
|
|
"""Initializes an instance of LlmFileHandler.
|