fix: keep logging available when rotation is blocked
This commit is contained in:
1 parent
3a353aa2d9
commit
12a9a8a692
2 files changed
+51
-14
No files matched your search
@@ -61,23 +61,30 @@ class RotatingTextOutput(io.TextIOBase):
|
||||
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)
|
||||
self._append(data)
|
||||
return len(text)
|
||||
|
||||
if self._size:
|
||||
size_before = self._size
|
||||
self._rotate()
|
||||
if self._size >= size_before:
|
||||
self._ensure_open()
|
||||
self._append(data)
|
||||
return len(text)
|
||||
offset = 0
|
||||
while offset < len(data):
|
||||
self._ensure_open()
|
||||
capacity = self._max_bytes - self._size
|
||||
if capacity <= 0:
|
||||
size_before = self._size
|
||||
self._rotate()
|
||||
if self._size >= size_before:
|
||||
self._ensure_open()
|
||||
self._append(data[offset:])
|
||||
break
|
||||
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:
|
||||
self._rotate()
|
||||
continue
|
||||
@@ -86,24 +93,32 @@ class RotatingTextOutput(io.TextIOBase):
|
||||
# case; normal caps split only at code-point boundaries.
|
||||
chunk_size = chunk_size or min(capacity, len(data) - offset)
|
||||
chunk = data[offset : offset + chunk_size]
|
||||
assert self._handle is not None
|
||||
self._handle.write(chunk)
|
||||
self._handle.flush()
|
||||
self._size += chunk_size
|
||||
self._append(chunk)
|
||||
offset += chunk_size
|
||||
if offset < len(data):
|
||||
size_before = self._size
|
||||
self._rotate()
|
||||
if self._size >= size_before:
|
||||
self._ensure_open()
|
||||
self._append(data[offset:])
|
||||
break
|
||||
return len(text)
|
||||
|
||||
@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."""
|
||||
end = min(len(data), capacity)
|
||||
end = min(len(data), offset + capacity)
|
||||
if end == len(data):
|
||||
return end
|
||||
while end > 0 and data[end] & 0xC0 == 0x80:
|
||||
return end - offset
|
||||
while end > offset and data[end] & 0xC0 == 0x80:
|
||||
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:
|
||||
with self._lock:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
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(
|
||||
tmp_path: Path,
|
||||
monkeypatch,
|
||||
|
||||
Reference in new issue
Block a user