Create logger.py

Co-authored-by: the-cult-of-integral <98130822+the-cult-of-integral@users.noreply.github.com>
This commit is contained in:
mouadessalim 2022-09-27 20:27:06 +01:00 committed by GitHub
parent 0b6bacbb72
commit 9ef5f32a3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

33
logger.py Normal file
View File

@ -0,0 +1,33 @@
import logging
from functools import wraps
from typing import Any
logging.basicConfig(filename='qrg.log', filemode='w',
format="[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s")
def log_unknown_exceptions(log_lvl: int) -> Any:
"""Logs unknown exceptions that occur in a function to a log file.
"""
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
try:
result = func(*args, **kwargs)
return result
except Exception as e:
if log_lvl == logging.DEBUG:
logging.debug(e)
elif log_lvl == logging.INFO:
logging.info(e)
elif log_lvl == logging.WARNING:
logging.warning(e)
elif log_lvl == logging.ERROR:
logging.error(e)
elif log_lvl == logging.CRITICAL:
logging.critical(e)
else:
raise ValueError('Invalid log level provided for log_unknown_exceptions decorator.')
return None
return wrapper
return decorate