comictagger/comictaggerlib/log.py

59 lines
1.7 KiB
Python
Raw Permalink Normal View History

2022-12-06 00:20:01 -08:00
from __future__ import annotations
import logging.handlers
import pathlib
import platform
import sys
from comictaggerlib.ctversion import version
2022-12-06 00:20:01 -08:00
logger = logging.getLogger("comictagger")
2022-11-29 15:14:53 -08:00
def get_filename(filename: str) -> str:
filename, _, number = filename.rpartition(".")
return filename.removesuffix("log") + number + ".log"
2022-12-06 00:20:01 -08:00
def get_file_handler(filename: pathlib.Path) -> logging.FileHandler:
file_handler = logging.handlers.RotatingFileHandler(filename, encoding="utf-8", backupCount=10)
2022-11-29 15:14:53 -08:00
file_handler.namer = get_filename
2022-12-06 00:20:01 -08:00
if filename.is_file() and filename.stat().st_size > 0:
file_handler.doRollover()
return file_handler
def setup_logging(verbose: int, log_dir: pathlib.Path) -> None:
logging.getLogger("comicapi").setLevel(logging.DEBUG)
logging.getLogger("comictaggerlib").setLevel(logging.DEBUG)
2022-11-27 11:36:26 -08:00
logging.getLogger("comictalker").setLevel(logging.DEBUG)
logging.getLogger("pyrate_limiter").setLevel(logging.DEBUG)
2022-12-06 00:20:01 -08:00
log_file = log_dir / "ComicTagger.log"
2022-11-27 11:17:58 -08:00
log_dir.mkdir(parents=True, exist_ok=True)
2022-12-06 00:20:01 -08:00
stream_handler = logging.StreamHandler()
file_handler = get_file_handler(log_file)
if verbose > 1:
stream_handler.setLevel(logging.DEBUG)
elif verbose > 0:
stream_handler.setLevel(logging.INFO)
else:
stream_handler.setLevel(logging.WARNING)
logging.basicConfig(
2022-12-15 20:21:53 -08:00
handlers=[stream_handler, file_handler],
2022-12-06 00:20:01 -08:00
level=logging.WARNING,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logger.info(
"ComicTagger Version: %s running on: %s PyInstaller: %s",
version,
platform.system(),
"Yes" if getattr(sys, "frozen", None) else "No",
)