5
5
import logging
6
6
import sys
7
7
import os
8
+ import re
8
9
from colorama import Fore , Style , init as colorama_init
9
10
10
11
# Initialize colorama with autoreset
@@ -48,20 +49,95 @@ def supports_color():
48
49
# Use color only if supported
49
50
USE_COLOR = supports_color ()
50
51
52
+ # Define subdued colors for less important logs
53
+ class Colors :
54
+ # Bright colors for important messages
55
+ BRIGHT_CYAN = Fore .CYAN
56
+ BRIGHT_GREEN = Fore .GREEN
57
+ BRIGHT_YELLOW = Fore .YELLOW
58
+ BRIGHT_RED = Fore .RED
59
+
60
+ # Subdued colors for regular logs
61
+ SUBDUED_DEBUG = Fore .BLUE + Style .DIM # Dimmed blue for debug
62
+ SUBDUED_INFO = Fore .WHITE + Style .DIM # Dimmed white for info
63
+ SUBDUED_WARNING = Fore .YELLOW + Style .DIM # Dimmed yellow for warnings
64
+ SUBDUED_ERROR = Fore .RED + Style .DIM # Dimmed red for errors
65
+
66
+ # Reset
67
+ RESET = Style .RESET_ALL
68
+
69
+ # Patterns for important log messages that should stand out
70
+ IMPORTANT_PATTERNS = [
71
+ # Server status messages
72
+ r'Server status changed to' ,
73
+ r'SERVER (READY|STARTING)' ,
74
+ r'Starting server on port' ,
75
+ r'Server startup' ,
76
+ r'Application startup complete' ,
77
+
78
+ # Model-related messages
79
+ r'Model loaded' ,
80
+ r'Loading model' ,
81
+ r'Model configuration' ,
82
+
83
+ # Error and warning messages
84
+ r'Error' ,
85
+ r'Exception' ,
86
+ r'Failed' ,
87
+ r'CRITICAL' ,
88
+ r'WARNING' ,
89
+
90
+ # Ngrok-related messages
91
+ r'NGROK TUNNEL ACTIVE' ,
92
+ r'Ngrok tunnel established' ,
93
+
94
+ # Other important messages
95
+ r'FastAPI application startup' ,
96
+ r'HuggingFace token' ,
97
+ r'SERVER READY' ,
98
+ r'INITIALIZING'
99
+ ]
100
+
101
+ # Compiled regex patterns for performance
102
+ IMPORTANT_REGEX = [re .compile (pattern , re .IGNORECASE ) for pattern in IMPORTANT_PATTERNS ]
103
+
51
104
# Define formatters
52
- class ColoredFormatter (logging .Formatter ):
53
- """Formatter that adds colors to log messages"""
54
- FORMATS = {
55
- logging .DEBUG : f'{ Fore .CYAN } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Style .RESET_ALL } ' ,
56
- logging .INFO : f'{ Fore .GREEN } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Style .RESET_ALL } ' ,
57
- logging .WARNING : f'{ Fore .YELLOW } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Style .RESET_ALL } ' ,
58
- logging .ERROR : f'{ Fore .RED } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Style .RESET_ALL } ' ,
59
- logging .CRITICAL : f'{ Fore .RED } { Style .BRIGHT } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Style .RESET_ALL } '
60
- }
105
+ class SubduedColoredFormatter (logging .Formatter ):
106
+ """Formatter that adds subdued colors to regular logs and bright colors to important logs"""
61
107
62
108
def format (self , record ):
63
- log_format = self .FORMATS .get (record .levelno )
64
- formatter = logging .Formatter (log_format )
109
+ # Check if this is an important message that should stand out
110
+ is_important = False
111
+
112
+ # Check message content against important patterns
113
+ if hasattr (record , 'msg' ) and isinstance (record .msg , str ):
114
+ for pattern in IMPORTANT_REGEX :
115
+ if pattern .search (record .msg ):
116
+ is_important = True
117
+ break
118
+
119
+ # Special case for certain types of logs - make them very subdued
120
+ is_ngrok_log = hasattr (record , 'name' ) and record .name .startswith ('pyngrok' )
121
+ is_uvicorn_log = hasattr (record , 'name' ) and record .name .startswith ('uvicorn' )
122
+
123
+ # Determine the appropriate color based on log level and importance
124
+ if is_ngrok_log or is_uvicorn_log :
125
+ # Make ngrok and uvicorn logs very subdued (dark gray)
126
+ color = Fore .BLACK + Style .BRIGHT # This creates a dark gray color on most terminals
127
+ elif record .levelno == logging .DEBUG :
128
+ color = Colors .BRIGHT_CYAN if is_important else Colors .SUBDUED_DEBUG
129
+ elif record .levelno == logging .INFO :
130
+ color = Colors .BRIGHT_GREEN if is_important else Colors .SUBDUED_INFO
131
+ elif record .levelno == logging .WARNING :
132
+ color = Colors .BRIGHT_YELLOW if is_important else Colors .SUBDUED_WARNING
133
+ elif record .levelno >= logging .ERROR : # ERROR and CRITICAL
134
+ color = Colors .BRIGHT_RED # Always use bright red for errors
135
+ else :
136
+ color = Colors .SUBDUED_INFO # Default
137
+
138
+ # Format with the appropriate color
139
+ formatted_message = f'{ color } %(asctime)s - %(name)s - %(levelname)s - %(message)s{ Colors .RESET } '
140
+ formatter = logging .Formatter (formatted_message )
65
141
return formatter .format (record )
66
142
67
143
# Plain formatter without colors
@@ -86,7 +162,7 @@ def configure_root_logger():
86
162
handler = logging .StreamHandler (sys .stdout )
87
163
88
164
if USE_COLOR :
89
- handler .setFormatter (ColoredFormatter ())
165
+ handler .setFormatter (SubduedColoredFormatter ())
90
166
else :
91
167
handler .setFormatter (plain_formatter )
92
168
0 commit comments