Back
+30 XP
Concept

Size-Capped Circular Log File Rotator

Unbounded logging fills up server disks until applications crash with ENOSPC (No space left on device). A circular log rotator monitors file size, rotating active files into numbered backup generations (app.log.1, app.log.2) and purging old backups when size limits are exceeded.

python
# Shift backup generations:
os.replace('app.log.1', 'app.log.2')
os.replace('app.log', 'app.log.1')

# Size-Capped Circular Log File Rotator If your web server writes 20 GB of access logs per week without a rotation strategy, Sunday morning will arrive with a dead server and `OSError: [Errno 28] No space left on device`. 🛑📉 A **Size-Capped Circular Rotator** bounds disk usage: - When `app.log` exceeds `max_bytes`, rotate: - Oldest backup beyond `max_backups` is purged. - `app.log.2` is renamed to `app.log.3`. - `app.log.1` is renamed to `app.log.2`. - Current `app.log` is renamed to `app.log.1`. - A fresh `app.log` is opened. Total disk usage is strictly capped at `max_bytes * (max_backups + 1)`. ```python import os # Always rename in reverse index order: for i in range(max_backups - 1, 0, -1): src = f"{base}.{i}" dst = f"{base}.{i + 1}" if os.path.exists(src): os.replace(src, dst) os.replace(base, f"{base}.1") ``` ## Pop Quiz Why does the standard rotating file handler shift backup indices in reverse order (e.g. 2 $\rightarrow$ 3, then 1 $\rightarrow$ 2)? - [ ] To avoid overwriting existing backup files before they are shifted to the next generation. - [ ] Because Linux file systems only allow file renaming in descending alphabetical order. - [ ] To force the operating system to compress older log archives using gzip. - [ ] Because ascending index renaming causes deadlocks on ext4 inode tables. ## Real-World Challenge Implement `SizeCappedLogRotator(base_path: str, max_bytes: int, max_backups: int)`. - Method `append_line(text: str)`: Appends `text + "\n"` to `base_path`. - If appending the line causes file size to exceed `max_bytes`, trigger rotation before writing. - Maintain at most `max_backups` backups (`.1`, `.2`, etc.). <details> <summary>Solution</summary> ```python import os class SizeCappedLogRotator: def __init__(self, base_path: str, max_bytes: int, max_backups: int): self.base_path = base_path self.max_bytes = max_bytes self.max_backups = max_backups def append_line(self, text: str): line_bytes = (text + "\n").encode("utf-8") current_size = os.path.getsize(self.base_path) if os.path.exists(self.base_path) else 0 if current_size + len(line_bytes) > self.max_bytes: self._rotate() with open(self.base_path, "ab") as f: f.write(line_bytes) def _rotate(self): if not os.path.exists(self.base_path): return # Purge oldest file if it exceeds max_backups oldest_backup = f"{self.base_path}.{self.max_backups}" if os.path.exists(oldest_backup): try: os.remove(oldest_backup) except OSError: pass # Shift existing backups downward in reverse order for i in range(self.max_backups - 1, 0, -1): src = f"{self.base_path}.{i}" dst = f"{self.base_path}.{i + 1}" if os.path.exists(src): os.replace(src, dst) os.replace(self.base_path, f"{self.base_path}.1") # Verification test_file = "/tmp/test_rotating_app.log" # Cleanup any previous test artifacts for f in [test_file, f"{test_file}.1", f"{test_file}.2", f"{test_file}.3"]: if os.path.exists(f): os.remove(f) rotator = SizeCappedLogRotator(test_file, max_bytes=35, max_backups=2) rotator.append_line("Message 1: 0123456789") # ~23 bytes rotator.append_line("Message 2: 0123456789") # Exceeds 35 -> triggers rotation! assert os.path.exists(f"{test_file}.1") assert os.path.exists(test_file) # Cleanup for f in [test_file, f"{test_file}.1", f"{test_file}.2"]: if os.path.exists(f): os.remove(f) ``` </details>