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

+59
View File
@@ -0,0 +1,59 @@
"""Shared configuration for human-readable nanobot logs."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from loguru import logger
if TYPE_CHECKING:
from loguru import Record
LOG_FORMAT = (
"<green>{time:YYYY-MM-DDTHH:mm:ss.SSSZZ}</green> | "
"<level>{level: <7}</level> | "
"<cyan>{extra[channel]}</cyan> | "
"<cyan>{extra[log_context]}</cyan> | "
"<level>{extra[safe_message]}</level>"
)
def _single_line(value: object) -> str:
return str(value).replace("\r", "\\r").replace("\n", "\\n")
def prepare_log_record(record: "Record") -> bool:
"""Populate display fields without altering the structured log record."""
extra = record["extra"]
extra.setdefault("channel", "-")
extra["channel"] = _single_line(extra["channel"])
extra["safe_message"] = _single_line(record["message"])
context: list[str] = []
for label, field in (
("request", "request_id"),
("turn", "turn_id"),
("session", "session_key"),
):
value = extra.get(field)
if value not in (None, "", "-"):
context.append(f"{label}={_single_line(value)}")
extra["log_context"] = " ".join(context) or "-"
return True
def add_console_log_sink(stream: Any, *, level: str = "INFO") -> int:
"""Add the standard nanobot console sink and return its handler id."""
return logger.add(
stream,
format=LOG_FORMAT,
level=level,
colorize=None,
filter=prepare_log_record,
)
def configure_console_logging(stream: Any, *, level: str = "INFO") -> int:
"""Replace existing handlers with the standard nanobot console sink."""
logger.remove()
return add_console_log_sink(stream, level=level)
+168
View File
@@ -0,0 +1,168 @@
"""Bounded text output for detached nanobot processes."""
from __future__ import annotations
import io
import os
import shutil
import sys
import threading
from contextlib import suppress
from pathlib import Path
from typing import BinaryIO
BACKGROUND_LOG_PATH_ENV = "_NANOBOT_BACKGROUND_LOG_PATH"
BACKGROUND_LOG_MAX_BYTES_ENV = "_NANOBOT_BACKGROUND_LOG_MAX_BYTES"
BACKGROUND_LOG_BACKUP_COUNT_ENV = "_NANOBOT_BACKGROUND_LOG_BACKUP_COUNT"
DEFAULT_MAX_BYTES = 10 * 1024 * 1024
DEFAULT_BACKUP_COUNT = 5
class RotatingTextOutput(io.TextIOBase):
"""A UTF-8 text stream that rotates a file before it exceeds its size cap."""
def __init__(self, path: Path, *, max_bytes: int, backup_count: int) -> None:
super().__init__()
self._path = path
self._max_bytes = max(1, max_bytes)
self._backup_count = max(0, backup_count)
self._handle: BinaryIO | None = None
self._size = 0
self._lock = threading.RLock()
@property
def encoding(self) -> str: # pyright: ignore[reportIncompatibleVariableOverride]
return "utf-8"
@property
def errors(self) -> str: # pyright: ignore[reportIncompatibleVariableOverride]
return "replace"
def writable(self) -> bool:
return True
def isatty(self) -> bool:
return False
def fileno(self) -> int:
with self._lock:
self._ensure_open()
assert self._handle is not None
return self._handle.fileno()
def write(self, text: str) -> int:
if not text:
return 0
data = text.encode(self.encoding, errors=self.errors)
with self._lock:
self._ensure_open()
if self._size and self._size + len(data) > self._max_bytes:
self._rotate()
self._ensure_open()
assert self._handle is not None
self._handle.write(data)
self._handle.flush()
self._size += len(data)
return len(text)
def flush(self) -> None:
with self._lock:
if self._handle is not None:
self._handle.flush()
def close(self) -> None:
with self._lock:
if self._handle is not None:
self._handle.close()
self._handle = None
super().close()
def _ensure_open(self) -> None:
if self._handle is not None:
return
self._path.parent.mkdir(parents=True, exist_ok=True)
size = self._path.stat().st_size if self._path.exists() else 0
if size >= self._max_bytes:
self._rotate()
size = self._path.stat().st_size if self._path.exists() else 0
self._handle = self._path.open("ab")
self._size = size
def _rotate(self) -> None:
if self._handle is not None:
self._handle.flush()
self._handle.close()
self._handle = None
if self._backup_count == 0:
try:
self._path.unlink(missing_ok=True)
self._size = 0
except OSError:
self._size = self._path.stat().st_size if self._path.exists() else 0
return
try:
oldest = self._backup_path(self._backup_count)
oldest.unlink(missing_ok=True)
for index in range(self._backup_count - 1, 0, -1):
source = self._backup_path(index)
if source.exists():
source.replace(self._backup_path(index + 1))
if self._path.exists():
try:
self._path.replace(self._backup_path(1))
except PermissionError:
# A Windows ``gateway logs --follow`` reader can prevent rename.
# Copy and truncate preserves both retention and the live stream.
shutil.copyfile(self._path, self._backup_path(1))
self._path.open("wb").close()
self._size = 0
except OSError:
# Logging must remain available even when rotation is temporarily blocked.
self._size = self._path.stat().st_size if self._path.exists() else 0
def _backup_path(self, index: int) -> Path:
return self._path.with_name(f"{self._path.name}.{index}")
def _positive_int(value: str | None, default: int, *, minimum: int) -> int:
try:
return max(minimum, int(value or ""))
except ValueError:
return default
def configure_background_output_from_env() -> RotatingTextOutput | None:
"""Replace inherited output handles when launched by ``ManagedProcessRuntime``."""
raw_path = os.environ.pop(BACKGROUND_LOG_PATH_ENV, "")
if not raw_path:
return None
max_bytes = _positive_int(
os.environ.pop(BACKGROUND_LOG_MAX_BYTES_ENV, None),
DEFAULT_MAX_BYTES,
minimum=1,
)
backup_count = _positive_int(
os.environ.pop(BACKGROUND_LOG_BACKUP_COUNT_ENV, None),
DEFAULT_BACKUP_COUNT,
minimum=0,
)
stream = RotatingTextOutput(
Path(raw_path),
max_bytes=max_bytes,
backup_count=backup_count,
)
replaced = {id(output): output for output in (sys.stdout, sys.stderr)}
sys.stdout = stream
sys.stderr = stream
sys.__stdout__ = stream
sys.__stderr__ = stream
for output in replaced.values():
with suppress(Exception):
output.flush()
with suppress(Exception):
output.close()
return stream