Add rotating file logging
Logs go to bot.log in the project root by default (10 MB, 3 backups). Set LOG_FILE env var to override the path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
30
main.py
30
main.py
@@ -2,26 +2,46 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import logging.handlers
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from app.config import load_app_config
|
from app.config import load_app_config
|
||||||
from app.manager import AccountManager
|
from app.manager import AccountManager
|
||||||
|
|
||||||
|
LOG_FORMAT = "%(asctime)s | %(levelname)s | %(name)s | %(message)s"
|
||||||
|
|
||||||
def configure_logging() -> None:
|
|
||||||
|
def configure_logging(root: Path) -> None:
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
format=LOG_FORMAT,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
log_file_env = os.environ.get("LOG_FILE")
|
||||||
|
log_path = Path(log_file_env).expanduser() if log_file_env else root / "bot.log"
|
||||||
|
if not log_path.is_absolute():
|
||||||
|
log_path = (root / log_path).resolve()
|
||||||
|
|
||||||
|
file_handler = logging.handlers.RotatingFileHandler(
|
||||||
|
log_path,
|
||||||
|
maxBytes=10 * 1024 * 1024,
|
||||||
|
backupCount=3,
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
|
||||||
|
logging.getLogger().addHandler(file_handler)
|
||||||
|
|
||||||
|
|
||||||
|
_ROOT = Path(__file__).resolve().parent
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
root = Path(__file__).resolve().parent
|
config = load_app_config(_ROOT)
|
||||||
config = load_app_config(root)
|
|
||||||
manager = AccountManager(config)
|
manager = AccountManager(config)
|
||||||
await manager.run()
|
await manager.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
configure_logging()
|
configure_logging(_ROOT)
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user