feat(log): add logger

This commit is contained in:
MingxuanGame
2025-07-28 10:38:40 +00:00
parent e369944d87
commit 1be3388524
13 changed files with 212 additions and 174 deletions

View File

@@ -3,10 +3,10 @@ from __future__ import annotations
from abc import abstractmethod
import asyncio
import time
import traceback
from typing import Any
from app.config import settings
from app.log import logger
from app.models.signalr import UserState
from app.signalr.exception import InvokeException
from app.signalr.packet import (
@@ -79,7 +79,7 @@ class Client:
except WebSocketDisconnect:
break
except Exception as e:
print(f"Error in ping task for {self.connection_id}: {e}")
logger.error(f"Error in ping task for {self.connection_id}: {e}")
break
@@ -205,22 +205,24 @@ class Hub[TState: UserState]:
self.tasks.add(task)
task.add_done_callback(self.tasks.discard)
except WebSocketDisconnect as e:
print(f"Client {client.connection_id} disconnected: {e.code}, {e.reason}")
logger.info(
f"Client {client.connection_id} disconnected: {e.code}, {e.reason}"
)
except RuntimeError as e:
if "disconnect message" in str(e):
print(f"Client {client.connection_id} closed the connection.")
logger.info(f"Client {client.connection_id} closed the connection.")
else:
traceback.print_exc()
print(f"RuntimeError in client {client.connection_id}: {e}")
logger.exception(f"RuntimeError in client {client.connection_id}: {e}")
except CloseConnection as e:
if not e.from_client:
await client.send_packet(
ClosePacket(error=e.message, allow_reconnect=e.allow_reconnect)
)
print(f"Client {client.connection_id} closed the connection: {e.message}")
except Exception as e:
traceback.print_exc()
print(f"Error in client {client.connection_id}: {e}")
logger.info(
f"Client {client.connection_id} closed the connection: {e.message}"
)
except Exception:
logger.exception(f"Error in client {client.connection_id}")
await self.remove_client(client)
@@ -236,7 +238,10 @@ class Hub[TState: UserState]:
except InvokeException as e:
error = e.message
except Exception as e:
traceback.print_exc()
logger.exception(
f"Error invoking method {packet.target} for "
f"client {client.connection_id}"
)
error = str(e)
if packet.invocation_id is not None:
await client.send_packet(

View File

@@ -84,7 +84,6 @@ class MetadataHub(Hub[MetadataClientState]):
if (
friend_state := self.state.get(friend_id)
) and friend_state.pushable:
print("Pushed")
tasks.append(
self.broadcast_group_call(
self.friend_presence_watchers_group(friend_id),

View File

@@ -319,7 +319,6 @@ class SpectatorHub(Hub[StoreClientState]):
)
async def StartWatchingUser(self, client: Client, target_id: int) -> None:
print(f"StartWatchingUser -> {client.connection_id} {target_id}")
user_id = int(client.connection_id)
target_store = self.get_or_create_state(client)
if target_store.state:
@@ -347,7 +346,6 @@ class SpectatorHub(Hub[StoreClientState]):
)
async def EndWatchingUser(self, client: Client, target_id: int) -> None:
print(f"EndWatchingUser -> {client.connection_id} {target_id}")
user_id = int(client.connection_id)
self.remove_from_group(client, self.group_id(target_id))
store = self.state.get(user_id)