log formatting
This commit is contained in:
31
main.py
31
main.py
@@ -7,6 +7,7 @@ from importlib import metadata
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from auth.oauth_config import reload_oauth_config
|
||||
from core.log_formatter import EnhancedLogFormatter
|
||||
from core.utils import check_credentials_directory_permissions
|
||||
from core.server import server, set_transport_mode, configure_server_for_http
|
||||
from core.tool_tier_loader import resolve_tools_from_tier
|
||||
@@ -59,27 +60,23 @@ def safe_print(text):
|
||||
print(text.encode('ascii', errors='replace').decode(), file=sys.stderr)
|
||||
|
||||
def configure_safe_logging():
|
||||
"""Configure logging to handle Unicode safely on Windows."""
|
||||
class SafeFormatter(logging.Formatter):
|
||||
class SafeEnhancedFormatter(EnhancedLogFormatter):
|
||||
"""Enhanced ASCII formatter with additional Windows safety."""
|
||||
def format(self, record):
|
||||
try:
|
||||
return super().format(record)
|
||||
except UnicodeEncodeError:
|
||||
# Replace the message with ASCII-safe version
|
||||
record.msg = str(record.msg).encode('ascii', errors='replace').decode('ascii')
|
||||
return super().format(record)
|
||||
|
||||
# Replace all handlers' formatters with safe ones
|
||||
# Fallback to ASCII-safe formatting
|
||||
service_prefix = self._get_ascii_prefix(record.name, record.levelname)
|
||||
safe_msg = str(record.getMessage()).encode('ascii', errors='replace').decode('ascii')
|
||||
return f"{service_prefix} {safe_msg}"
|
||||
|
||||
# Replace all console handlers' formatters with safe enhanced ones
|
||||
for handler in logging.root.handlers:
|
||||
original_formatter = handler.formatter
|
||||
if original_formatter:
|
||||
safe_formatter = SafeFormatter(
|
||||
fmt=original_formatter._fmt if hasattr(original_formatter, '_fmt') else '%(message)s',
|
||||
datefmt=original_formatter.datefmt
|
||||
)
|
||||
else:
|
||||
safe_formatter = SafeFormatter()
|
||||
handler.setFormatter(safe_formatter)
|
||||
# Only apply to console/stream handlers, keep file handlers as-is
|
||||
if isinstance(handler, logging.StreamHandler) and handler.stream.name in ['<stderr>', '<stdout>']:
|
||||
safe_formatter = SafeEnhancedFormatter(use_colors=True)
|
||||
handler.setFormatter(safe_formatter)
|
||||
|
||||
def main():
|
||||
"""
|
||||
@@ -88,7 +85,7 @@ def main():
|
||||
"""
|
||||
# Configure safe logging for Windows Unicode handling
|
||||
configure_safe_logging()
|
||||
|
||||
|
||||
# Parse command line arguments
|
||||
parser = argparse.ArgumentParser(description='Google Workspace MCP Server')
|
||||
parser.add_argument('--single-user', action='store_true',
|
||||
|
||||
Reference in New Issue
Block a user