Improve logging helper

This commit is contained in:
sfigato 2022-08-25 13:11:33 +02:00
parent 89abbff74d
commit e9e3958d2b
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 25 additions and 4 deletions

View File

@ -1,17 +1,23 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import functools
import logging import logging
import sys
import typing as T import typing as T
def logit(logger: logging.Logger) -> T.Callable[..., T.Any]: def logit(logger: logging.Logger) -> T.Callable[..., T.Any]:
def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]: def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]:
@functools.wraps(func)
def inner(*args, **kwargs): def inner(*args, **kwargs):
try: try:
value = func(*args, **kwargs) value = func(*args, **kwargs)
logger.info(f"Success: {func.__name__}(args={args}, kwargs={kwargs})") logger.info(f"Success: {func.__name__}")
logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})")
return value return value
except Exception as e: except Exception as e:
logger.error(f"Failure: {e}") logger.error(f"Failure: {e}")
logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})")
sys.exit(1)
return inner return inner
@ -25,16 +31,19 @@ def retry_and_log(logger: logging.Logger, retries: int):
""" """
def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]: def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]:
@functools.wraps(func)
def inner(*args, **kwargs): def inner(*args, **kwargs):
for i in range(retries): for i in range(retries):
try: try:
value = func(*args, **kwargs) value = func(*args, **kwargs)
logger.info( logger.info(f"Success ({i+1} tentative): {func.__name__}")
f"Success ({i+1} tentative): {func.__name__}(args={args}, kwargs={kwargs})" # noqa: E501 logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})")
)
return value return value
except Exception as e: except Exception as e:
logger.error(f"Failure ({i+1} tentative): {e}") logger.error(f"Failure ({i+1} tentative): {e}")
logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})")
sys.exit(2)
return inner return inner

View File

@ -1,9 +1,12 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
import logging
import re import re
import typing as T import typing as T
from latecomers.helpers import logit
from lxml import etree as et from lxml import etree as et
TIME_RE = re.compile(r"\d\d?:\d\d") TIME_RE = re.compile(r"\d\d?:\d\d")
@ -11,6 +14,8 @@ AIRPORT_RE = re.compile(r"[\w\d\s\S]+")
STATUS_RE = re.compile(r"(Arrivato|In Arrivo|Schedulato|Cancellato)") STATUS_RE = re.compile(r"(Arrivato|In Arrivo|Schedulato|Cancellato)")
PARSER = et.HTMLParser() PARSER = et.HTMLParser()
logger = logging.getLogger(__name__)
def not_empty(obj: et._Element) -> bool: def not_empty(obj: et._Element) -> bool:
if type(obj) is et._Element: if type(obj) is et._Element:
@ -20,6 +25,7 @@ def not_empty(obj: et._Element) -> bool:
raise RuntimeError(f"provided argument is of unsupported type: {type(obj)}") raise RuntimeError(f"provided argument is of unsupported type: {type(obj)}")
@logit(logger)
def find_table(html_content: T.Text) -> T.List[et._ElementTree]: def find_table(html_content: T.Text) -> T.List[et._ElementTree]:
""" """
Find the table that holds the data in the html response Find the table that holds the data in the html response

File diff suppressed because one or more lines are too long