fix: keep logging available when rotation is blocked

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-21 17:36:24 +08:00
1 parent 3a353aa2d9
commit 12a9a8a692
2 files changed
+51 -14

No files matched your search

+29 -14
View File
@@ -61,23 +61,30 @@ class RotatingTextOutput(io.TextIOBase):
if self._size and self._size + len(data) > self._max_bytes: if self._size and self._size + len(data) > self._max_bytes:
self._rotate() self._rotate()
self._ensure_open() self._ensure_open()
assert self._handle is not None self._append(data)
self._handle.write(data)
self._handle.flush()
self._size += len(data)
return len(text) return len(text)
if self._size: if self._size:
size_before = self._size
self._rotate() self._rotate()
if self._size >= size_before:
self._ensure_open()
self._append(data)
return len(text)
offset = 0 offset = 0
while offset < len(data): while offset < len(data):
self._ensure_open() self._ensure_open()
capacity = self._max_bytes - self._size capacity = self._max_bytes - self._size
if capacity <= 0: if capacity <= 0:
size_before = self._size
self._rotate() self._rotate()
if self._size >= size_before:
self._ensure_open()
self._append(data[offset:])
break
continue continue
chunk_size = self._utf8_chunk_size(data[offset:], capacity) chunk_size = self._utf8_chunk_size(data, offset, capacity)
if chunk_size == 0 and self._size: if chunk_size == 0 and self._size:
self._rotate() self._rotate()
continue continue
@@ -86,24 +93,32 @@ class RotatingTextOutput(io.TextIOBase):
# case; normal caps split only at code-point boundaries. # case; normal caps split only at code-point boundaries.
chunk_size = chunk_size or min(capacity, len(data) - offset) chunk_size = chunk_size or min(capacity, len(data) - offset)
chunk = data[offset : offset + chunk_size] chunk = data[offset : offset + chunk_size]
assert self._handle is not None self._append(chunk)
self._handle.write(chunk)
self._handle.flush()
self._size += chunk_size
offset += chunk_size offset += chunk_size
if offset < len(data): if offset < len(data):
size_before = self._size
self._rotate() self._rotate()
if self._size >= size_before:
self._ensure_open()
self._append(data[offset:])
break
return len(text) return len(text)
@staticmethod @staticmethod
def _utf8_chunk_size(data: bytes, capacity: int) -> int: def _utf8_chunk_size(data: bytes, offset: int, capacity: int) -> int:
"""Return the largest prefix within capacity that ends on a UTF-8 boundary.""" """Return the largest prefix within capacity that ends on a UTF-8 boundary."""
end = min(len(data), capacity) end = min(len(data), offset + capacity)
if end == len(data): if end == len(data):
return end return end - offset
while end > 0 and data[end] & 0xC0 == 0x80: while end > offset and data[end] & 0xC0 == 0x80:
end -= 1 end -= 1
return end return end - offset
def _append(self, data: bytes) -> None:
assert self._handle is not None
self._handle.write(data)
self._handle.flush()
self._size += len(data)
def flush(self) -> None: def flush(self) -> None:
with self._lock: with self._lock:
+22
View File
@@ -53,6 +53,28 @@ def test_rotating_output_splits_one_oversized_write_at_utf8_boundaries(
assert "".join(path.read_text(encoding="utf-8") for path in paths) == text assert "".join(path.read_text(encoding="utf-8") for path in paths) == text
def test_rotating_output_keeps_writing_when_rotation_is_blocked(
tmp_path: Path,
monkeypatch,
) -> None:
log_path = tmp_path / "gateway.log"
output = RotatingTextOutput(log_path, max_bytes=8, backup_count=1)
output.write("12345678")
original_replace = Path.replace
def block_active_log_rotation(path: Path, target: Path) -> Path:
if path == log_path:
raise OSError("rotation blocked")
return original_replace(path, target)
monkeypatch.setattr(Path, "replace", block_active_log_rotation)
assert output.write("oversized output") == len("oversized output")
output.close()
assert log_path.read_text(encoding="utf-8") == "12345678oversized output"
def test_rotating_output_falls_back_when_active_log_cannot_be_renamed( def test_rotating_output_falls_back_when_active_log_cannot_be_renamed(
tmp_path: Path, tmp_path: Path,
monkeypatch, monkeypatch,