Initial version of the E-Invoice solution of JR IT Services
This commit is contained in:
30
jr_einvoice/logging_utils.py
Normal file
30
jr_einvoice/logging_utils.py
Normal file
@@ -0,0 +1,30 @@
|
||||
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
|
||||
Reference in New Issue
Block a user