diff --git a/.gitignore b/.gitignore index 6b7cb8e..c1725f8 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,7 @@ config.min.ini mctomqtt.py bot_cli.py bot_start_time.txt +*.log.* # Test files and development artifacts (root level) test_*.py diff --git a/config.ini.example b/config.ini.example index 48b856a..49d297b 100644 --- a/config.ini.example +++ b/config.ini.example @@ -389,6 +389,7 @@ log_level = INFO # Log file path (leave empty for console only) # Bot will write logs to this file in addition to console +# Logs rotate at 5 MB with up to 3 backup files (e.g. meshcore_bot.log.1, .2, .3) log_file = meshcore_bot.log # Enable colored console output diff --git a/config.ini.minimal-example b/config.ini.minimal-example index 638b610..680e237 100644 --- a/config.ini.minimal-example +++ b/config.ini.minimal-example @@ -231,6 +231,7 @@ log_level = INFO # Log file path (leave empty for console only) # Bot will write logs to this file in addition to console +# Logs rotate at 5 MB with up to 3 backup files (e.g. meshcore_bot.log.1, .2, .3) log_file = meshcore_bot.log # Enable colored console output diff --git a/config.ini.quickstart b/config.ini.quickstart index 0c29bb3..b95544c 100644 --- a/config.ini.quickstart +++ b/config.ini.quickstart @@ -35,6 +35,7 @@ help = "Bot Help: test (or t), ping, help, hello, cmd, advert, wx, aqi, sun, moo [Logging] log_level = INFO +# Rotates at 5 MB, keeps 3 backups (e.g. meshcore_bot.log.1, .2, .3) log_file = meshcore_bot.log # Weather (for wx command) - set default_state to your region diff --git a/docs/data-retention.md b/docs/data-retention.md index 1cb8a05..c38ee61 100644 --- a/docs/data-retention.md +++ b/docs/data-retention.md @@ -43,3 +43,5 @@ Shorter retention (e.g. 2–3 days for `packet_stream`) is enough for the web vi - Deletes old rows from **mesh_connections** (mesh graph). So as long as the bot is running, the database is pruned on a schedule regardless of whether you run the standalone web viewer or the stats command. + +**Log files** (`[Logging] log_file`, e.g. `meshcore_bot.log`) use rotating file logging: the bot rotates at 5 MB and keeps up to 3 backup files (same policy as the web viewer), so log disk use stays bounded. diff --git a/modules/core.py b/modules/core.py index a27b0fd..52b9a30 100644 --- a/modules/core.py +++ b/modules/core.py @@ -8,6 +8,7 @@ import asyncio import configparser import json import logging +from logging.handlers import RotatingFileHandler import colorlog import time import threading @@ -862,7 +863,12 @@ long_jokes = false if log_file: try: - file_handler = logging.FileHandler(log_file) + file_handler = RotatingFileHandler( + log_file, + maxBytes=5 * 1024 * 1024, + backupCount=3, + encoding='utf-8', + ) file_handler.setFormatter(formatter) self.logger.addHandler(file_handler) except (OSError, PermissionError) as e: