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

+40
View File
@@ -0,0 +1,40 @@
from io import StringIO
from loguru import logger
from nanobot.utils.log_config import add_console_log_sink
def test_console_log_sink_includes_context_and_escapes_message_newlines() -> None:
output = StringIO()
sink = add_console_log_sink(output, level="DEBUG")
try:
with logger.contextualize(
request_id="request-1",
turn_id="turn-1",
session_key="channel:chat",
):
logger.bind(channel="test").info("first line\nsecond line")
finally:
logger.remove(sink)
rendered = output.getvalue()
assert "request=request-1 turn=turn-1 session=channel:chat" in rendered
assert "first line\\nsecond line" in rendered
assert "first line\nsecond line" not in rendered
def test_console_log_sink_keeps_exception_traceback_multiline() -> None:
output = StringIO()
sink = add_console_log_sink(output)
try:
try:
raise RuntimeError("broken")
except RuntimeError:
logger.opt(exception=True).error("operation failed")
finally:
logger.remove(sink)
rendered = output.getvalue()
assert "operation failed" in rendered
assert "RuntimeError: broken" in rendered
+94
View File
@@ -0,0 +1,94 @@
import os
import subprocess
import sys
from pathlib import Path
from nanobot.utils.rotating_output import (
BACKGROUND_LOG_BACKUP_COUNT_ENV,
BACKGROUND_LOG_MAX_BYTES_ENV,
BACKGROUND_LOG_PATH_ENV,
RotatingTextOutput,
)
def test_rotating_output_enforces_size_and_backup_count(tmp_path: Path) -> None:
log_path = tmp_path / "gateway.log"
output = RotatingTextOutput(log_path, max_bytes=8, backup_count=2)
output.write("12345678")
output.write("new")
output.write("abcdefgh")
output.close()
assert log_path.read_text(encoding="utf-8") == "abcdefgh"
assert (tmp_path / "gateway.log.1").read_text(encoding="utf-8") == "new"
assert (tmp_path / "gateway.log.2").read_text(encoding="utf-8") == "12345678"
assert not (tmp_path / "gateway.log.3").exists()
def test_rotating_output_rotates_oversized_existing_file_before_append(tmp_path: Path) -> None:
log_path = tmp_path / "gateway.log"
log_path.write_text("existing output", encoding="utf-8")
output = RotatingTextOutput(log_path, max_bytes=8, backup_count=1)
output.write("fresh")
output.close()
assert log_path.read_text(encoding="utf-8") == "fresh"
assert (tmp_path / "gateway.log.1").read_text(encoding="utf-8") == "existing output"
def test_rotating_output_falls_back_when_active_log_cannot_be_renamed(
tmp_path: Path,
monkeypatch,
) -> None:
log_path = tmp_path / "gateway.log"
log_path.write_text("existing", encoding="utf-8")
original_replace = Path.replace
def fail_active_log_rename(path: Path, target: Path) -> Path:
if path == log_path:
raise PermissionError("file is being followed")
return original_replace(path, target)
monkeypatch.setattr(Path, "replace", fail_active_log_rename)
output = RotatingTextOutput(log_path, max_bytes=8, backup_count=1)
output.write("fresh")
output.close()
assert log_path.read_text(encoding="utf-8") == "fresh"
assert (tmp_path / "gateway.log.1").read_text(encoding="utf-8") == "existing"
def test_background_output_environment_redirects_stdout_and_stderr(tmp_path: Path) -> None:
log_path = tmp_path / "gateway.log"
env = os.environ.copy()
env[BACKGROUND_LOG_PATH_ENV] = str(log_path)
env[BACKGROUND_LOG_MAX_BYTES_ENV] = "1024"
env[BACKGROUND_LOG_BACKUP_COUNT_ENV] = "1"
result = subprocess.run(
[
sys.executable,
"-c",
(
"from nanobot.utils.rotating_output import "
"configure_background_output_from_env; "
"configure_background_output_from_env(); "
"print('stdout line'); "
"print('stderr line', file=__import__('sys').stderr)"
),
],
check=True,
capture_output=True,
env=env,
text=True,
)
assert result.stdout == ""
assert result.stderr == ""
assert log_path.read_text(encoding="utf-8").splitlines() == [
"stdout line",
"stderr line",
]