comictagger/comictaggerlib/log.py

43 lines
1.2 KiB
Python
Raw Normal View History

2022-12-06 00:20:01 -08:00
from __future__ import annotations
import logging.handlers
import pathlib
logger = logging.getLogger("comictagger")
def get_file_handler(filename: pathlib.Path) -> logging.FileHandler:
file_handler = logging.handlers.RotatingFileHandler(filename, encoding="utf-8", backupCount=10)
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)
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(
handlers=[
stream_handler,
file_handler,
],
level=logging.WARNING,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)