4
4
5
5
Default Logging Configuration
6
6
"""
7
+ # pylint: disable=too-few-public-methods
7
8
# spell-checker:ignore levelname inotify openai httpcore fsevents litellm
8
9
9
10
import os
11
+ import asyncio
10
12
import logging
11
13
from logging .config import dictConfig
12
14
from common ._version import __version__
@@ -20,6 +22,30 @@ def filter(self, record):
20
22
return True
21
23
22
24
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
+
23
49
# Standard formatter
24
50
FORMATTER = {
25
51
"format" : "%(asctime)s (v%(__version__)s) - %(levelname)-8s - (%(name)s): %(message)s" ,
@@ -33,9 +59,8 @@ def filter(self, record):
33
59
"standard" : FORMATTER ,
34
60
},
35
61
"filters" : {
36
- "version_filter" : {
37
- "()" : VersionFilter ,
38
- },
62
+ "version_filter" : {"()" : VersionFilter },
63
+ "prettify_cancelled" : {"()" : PrettifyCancelledError },
39
64
},
40
65
"handlers" : {
41
66
"default" : {
@@ -56,13 +81,19 @@ def filter(self, record):
56
81
"level" : LOG_LEVEL ,
57
82
"handlers" : ["default" ],
58
83
"propagate" : False ,
84
+ "filters" : ["prettify_cancelled" ],
59
85
},
60
86
"uvicorn.access" : {
61
87
"level" : LOG_LEVEL ,
62
88
"handlers" : ["default" ],
63
89
"propagate" : False ,
64
90
},
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
+ },
66
97
"watchdog.observers.inotify_buffer" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
67
98
"PIL" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
68
99
"fsevents" : {"level" : "INFO" , "handlers" : ["default" ], "propagate" : False },
0 commit comments