Files
einvoice/jr_einvoice/logging_utils.py

31 lines
881 B
Python

from __future__ import annotations
import logging
from pathlib import Path
from typing import Optional
def setup_logging(log_level: str, log_file: Optional[Path]) -> logging.Logger:
logger = logging.getLogger("jr_einvoice")
logger.setLevel(logging.DEBUG)
if list(logger.handlers):
return logger
fmt = logging.Formatter(
fmt="%(asctime)s.%(msecs)03d %(levelname)s [%(name)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
ch = logging.StreamHandler()
ch.setLevel(getattr(logging, log_level.upper(), logging.INFO))
ch.setFormatter(fmt)
logger.addHandler(ch)
if log_file is not None:
log_file.parent.mkdir(parents=True, exist_ok=True)
fh = logging.FileHandler(log_file, encoding="utf-8")
fh.setLevel(logging.DEBUG)
fh.setFormatter(fmt)
logger.addHandler(fh)
return logger