58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import logging.handlers
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from app.config import load_app_config
|
|
from app.manager import AccountManager
|
|
from app.pyrogram_compat import install_kurigram_compat
|
|
|
|
try:
|
|
import uvloop
|
|
except ImportError: # uvloop does not support Windows.
|
|
uvloop = None
|
|
|
|
LOG_FORMAT = "%(asctime)s | %(levelname)s | %(name)s | %(message)s"
|
|
|
|
|
|
def configure_logging(root: Path) -> None:
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
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:
|
|
install_kurigram_compat()
|
|
config = load_app_config(_ROOT)
|
|
manager = AccountManager(config)
|
|
await manager.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
configure_logging(_ROOT)
|
|
if uvloop is None:
|
|
asyncio.run(main())
|
|
else:
|
|
uvloop.run(main())
|