| 99 | |
| 100 | |
| 101 | class WebSocketLogHandler(logging.Handler): |
| 102 | def __init__(self, manager: WebSocketConnectionManager): |
| 103 | super().__init__() |
| 104 | self.manager = manager |
| 105 | self.formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") |
| 106 | |
| 107 | def emit(self, record: logging.LogRecord): |
| 108 | if self.manager and self.manager.active_connections: |
| 109 | try: |
| 110 | log_entry_str = self.format(record) |
| 111 | try: |
| 112 | current_loop = asyncio.get_running_loop() |
| 113 | current_loop.create_task(self.manager.broadcast(log_entry_str)) |
| 114 | except RuntimeError: |
| 115 | pass |
| 116 | except Exception as e: |
| 117 | print(f"WebSocketLogHandler Error: Failed to broadcast log - {e}", file=sys.__stderr__) |
no outgoing calls