Skip to content

Commit c42ee07

Browse files
committed
Add rotating file handler for log file rotation (10MB, 5 backups)
Replace logging.FileHandler with RotatingFileHandler, add rotation settings and explanatory comments; keep UTF-8 encoding and DEBUG level for file logs.
1 parent dcf8764 commit c42ee07

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

brain_core/logging_config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import os
5+
from logging.handlers import RotatingFileHandler
56
from pathlib import Path
67

78
from rich.console import Console
@@ -69,14 +70,21 @@ def setup_logging(
6970
console_handler.setLevel(log_level)
7071
root_logger.addHandler(console_handler)
7172

72-
# File handler (if enabled)
73+
# File handler (if enabled) with rotation
7374
if enable_file_logging:
7475
if log_file is None:
7576
log_dir = Path.home() / ".brain" / "logs"
7677
log_dir.mkdir(parents=True, exist_ok=True)
7778
log_file = log_dir / "brain.log"
7879

79-
file_handler = logging.FileHandler(log_file, encoding="utf-8")
80+
# Rotating file handler: max 10MB per file, keep 5 backup files
81+
# This means max ~50MB of logs total (10MB current + 5x10MB backups)
82+
file_handler = RotatingFileHandler(
83+
log_file,
84+
maxBytes=10 * 1024 * 1024, # 10MB
85+
backupCount=5, # Keep 5 old files (brain.log.1, brain.log.2, etc.)
86+
encoding="utf-8",
87+
)
8088
file_handler.setLevel(logging.DEBUG) # File always captures everything
8189
file_handler.setFormatter(file_formatter)
8290
root_logger.addHandler(file_handler)

0 commit comments

Comments
 (0)