fix: improve log reliability and correlation

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-21 17:36:24 +08:00
1 parent 8eea1cb7a4
commit 8a7ca8a654
21 files changed
+698 -91

No files matched your search

+4 -3
View File
@@ -188,7 +188,9 @@ class NanobotDingTalkHandler(_CallbackHandlerBase):
)
return AckMessage.STATUS_OK, "OK"
self.channel.logger.info("Received message from {} ({}): {}", sender_name, sender_id, content)
self.channel.logger.debug(
"Received DingTalk message from {} ({})", sender_name, sender_id
)
# Forward to Nanobot via _on_message (non-blocking).
# Store reference to prevent GC before task completes.
@@ -367,7 +369,7 @@ class DingTalkChannel(BaseChannel):
if isawaitable(result):
await result
except Exception:
self.logger.debug("DingTalk stream client close failed", exc_info=True)
self.logger.opt(exception=True).debug("DingTalk stream client close failed")
async def _get_access_token(self) -> str | None:
"""Get or refresh Access Token."""
@@ -824,7 +826,6 @@ class DingTalkChannel(BaseChannel):
permission checks before publishing to the bus.
"""
try:
self.logger.info("inbound: {} from {}", content, sender_name)
if not sender_id:
self.logger.warning("dropping DingTalk message without a sender ID")
return
+12 -6
View File
@@ -536,7 +536,7 @@ class MatrixChannel(BaseChannel):
try:
response = await self.client.content_repository_config()
except Exception:
self.logger.error("Failed to fetch server upload limit", exc_info=True)
self.logger.opt(exception=True).error("Failed to fetch server upload limit")
return None
upload_size = getattr(response, "upload_size", None)
if isinstance(upload_size, int) and upload_size > 0:
@@ -582,7 +582,7 @@ class MatrixChannel(BaseChannel):
filesize=size_bytes,
)
except Exception:
self.logger.error("Matrix media upload failed for {}", filename, exc_info=True)
self.logger.opt(exception=True).error("Matrix media upload failed for {}", filename)
return fail
is_tuple_result = isinstance(cast(object, upload_result), tuple)
@@ -607,7 +607,9 @@ class MatrixChannel(BaseChannel):
try:
await self._send_room_content(room_id, content)
except Exception:
self.logger.error("Matrix room content send failed for room_id={}", room_id, exc_info=True)
self.logger.opt(exception=True).error(
"Matrix room content send failed for room_id={}", room_id
)
return fail
return None
@@ -712,7 +714,9 @@ class MatrixChannel(BaseChannel):
buf.text = previous_text
if created_buf:
self._stream_bufs.pop(stream_key, None)
self.logger.error("Stream send/edit failed for chat_id={}", chat_id, exc_info=True)
self.logger.opt(exception=True).error(
"Stream send/edit failed for chat_id={}", chat_id
)
await self._stop_typing_keepalive(chat_id, clear_typing=True)
raise
@@ -1011,7 +1015,9 @@ class MatrixChannel(BaseChannel):
),
)
except Exception:
self.logger.error("Matrix join request exception for room={}", room_id, exc_info=True)
self.logger.opt(exception=True).error(
"Matrix join request exception for room={}", room_id
)
return False
if isinstance(resp, JoinError):
self.logger.error("Matrix auto-join failed for room={}: {}", room_id, resp)
@@ -1287,7 +1293,7 @@ class MatrixChannel(BaseChannel):
except _MediaTooLargeError:
raise
except (aiohttp.ClientError, asyncio.TimeoutError, OSError):
self.logger.warning("download failed for {}", mxc_url, exc_info=True)
self.logger.opt(exception=True).warning("download failed for {}", mxc_url)
return None
def _decrypt_media_bytes(self, event: MatrixMediaEvent, ciphertext: bytes) -> bytes | None:
@@ -1838,8 +1838,9 @@ async def test_send_handles_upload_exception_and_reports_failure(tmp_path) -> No
client.room_send_calls[0]["content"]["body"]
== "Please review.\n[attachment: broken.txt - upload failed]"
)
channel.logger.error.assert_called_once_with(
"Matrix media upload failed for {}", "broken.txt", exc_info=True
channel.logger.opt.assert_called_once_with(exception=True)
channel.logger.opt.return_value.error.assert_called_once_with(
"Matrix media upload failed for {}", "broken.txt"
)
@@ -1861,10 +1862,10 @@ async def test_attachment_room_send_error_logs_room_id(tmp_path) -> None:
)
assert failure == "[attachment: report.txt - upload failed]"
channel.logger.error.assert_called_once_with(
channel.logger.opt.assert_called_once_with(exception=True)
channel.logger.opt.return_value.error.assert_called_once_with(
"Matrix room content send failed for room_id={}",
"!room:matrix.org",
exc_info=True,
)
@@ -2509,8 +2510,9 @@ async def test_send_delta_on_error_restores_buffer_and_raises(monkeypatch) -> No
assert len(client.room_send_calls) == 1
assert len(client.typing_calls) == 1
channel.logger.error.assert_called_once_with(
"Stream send/edit failed for chat_id={}", "!room:matrix.org", exc_info=True
channel.logger.opt.assert_called_once_with(exception=True)
channel.logger.opt.return_value.error.assert_called_once_with(
"Stream send/edit failed for chat_id={}", "!room:matrix.org"
)
client.raise_on_send = False
+1 -9
View File
@@ -624,10 +624,6 @@ class SignalChannel(BaseChannel):
if not self._running:
break
# Debug: log raw SSE lines (except keepalive pings)
if line and line != ":":
self.logger.debug("SSE line received: {}", line[:200])
# SSE format handling
if isinstance(line, str): # pyright: ignore[reportUnnecessaryIsInstance]
# Empty line signals end of event
@@ -641,7 +637,6 @@ class SignalChannel(BaseChannel):
if data is None:
self.logger.warning("Ignoring non-object SSE event: {}", data_str[:200])
continue
self.logger.debug("SSE event parsed: {}", data)
await self._handle_receive_notification(data)
except json.JSONDecodeError as e:
self.logger.warning(
@@ -690,13 +685,10 @@ class SignalChannel(BaseChannel):
async def _handle_receive_notification(self, params: dict[str, Any]) -> None:
"""Handle incoming message notification from signal-cli."""
self.logger.debug("_handle_receive_notification called with: {}", params)
async with self._safe_handle("receive notification", params):
# Extract envelope from SSE notification: {"envelope": {...}}
envelope = _as_json_object(params.get("envelope"))
self.logger.debug("Extracted envelope: {}", envelope)
if envelope is None:
self.logger.debug("No envelope found in params")
return
@@ -814,7 +806,7 @@ class SignalChannel(BaseChannel):
chat_id=chat_id,
)
self.logger.debug("Signal message from {}: {}...", sender_number, content[:50])
self.logger.debug("Received Signal message from {}", sender_number)
await self._start_typing(chat_id)
try:
+1 -1
View File
@@ -423,7 +423,7 @@ class WeixinChannel(BaseChannel):
self.config.base_url = base_url
return bool(self._token)
except Exception:
self.logger.error("Failed to load Weixin account state", exc_info=True)
self.logger.opt(exception=True).error("Failed to load Weixin account state")
return False
def _save_state(self, *, force: bool = False) -> None: