44
55Default Logging Configuration
66"""
7+ # pylint: disable=too-few-public-methods
78# spell-checker:ignore levelname inotify openai httpcore fsevents litellm
89
910import os
11+ import asyncio
1012import logging
1113from logging .config import dictConfig
1214from common ._version import __version__
@@ -20,6 +22,30 @@ def filter(self, record):
2022 return True
2123
2224
25+ class PrettifyCancelledError (logging .Filter ):
26+ """Filter that keeps the log but removes the traceback and replaces the message."""
27+
28+ def _contains_cancelled (self , exc : BaseException ) -> bool :
29+ if isinstance (exc , asyncio .CancelledError ):
30+ return True
31+ if hasattr (exc , "exceptions" ) and isinstance (exc , BaseExceptionGroup ): # type: ignore[name-defined]
32+ return any (self ._contains_cancelled (e ) for e in exc .exceptions ) # type: ignore[attr-defined]
33+ return False
34+
35+ def filter (self , record : logging .LogRecord ) -> bool :
36+ exc_info = record .__dict__ .get ("exc_info" )
37+ if not exc_info :
38+ return True
39+ _ , exc , _ = exc_info
40+ if exc and self ._contains_cancelled (exc ):
41+ # Strip the traceback and make it pretty
42+ record .exc_info = None
43+ record .msg = "Shutdown cancelled — graceful timeout exceeded."
44+ record .levelno = logging .WARNING
45+ record .levelname = logging .getLevelName (logging .WARNING )
46+ return True
47+
48+
2349# Standard formatter
2450FORMATTER = {
2551 "format" : "%(asctime)s (v%(__version__)s) - %(levelname)-8s - (%(name)s): %(message)s" ,
@@ -33,9 +59,8 @@ def filter(self, record):
3359 "standard" : FORMATTER ,
3460 },
3561 "filters" : {
36- "version_filter" : {
37- "()" : VersionFilter ,
38- },
62+ "version_filter" : {"()" : VersionFilter },
63+ "prettify_cancelled" : {"()" : PrettifyCancelledError },
3964 },
4065 "handlers" : {
4166 "default" : {
@@ -56,13 +81,19 @@ def filter(self, record):
5681 "level" : LOG_LEVEL ,
5782 "handlers" : ["default" ],
5883 "propagate" : False ,
84+ "filters" : ["prettify_cancelled" ],
5985 },
6086 "uvicorn.access" : {
6187 "level" : LOG_LEVEL ,
6288 "handlers" : ["default" ],
6389 "propagate" : False ,
6490 },
65- "asyncio" : {"level" : LOG_LEVEL , "handlers" : ["default" ], "propagate" : False },
91+ "asyncio" : {
92+ "level" : LOG_LEVEL ,
93+ "handlers" : ["default" ],
94+ "propagate" : False ,
95+ "filters" : ["prettify_cancelled" ],
96+ },
6697 "watchdog.observers.inotify_buffer" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
6798 "PIL" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
6899 "fsevents" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
0 commit comments