26 lines
879 B
Python
26 lines
879 B
Python
import logging
|
|
from logging.handlers import RotatingFileHandler
|
|
from typing import Literal
|
|
|
|
logger = logging.getLogger('card_creation')
|
|
logger.setLevel(logging.INFO)
|
|
handler = RotatingFileHandler(
|
|
filename='logs/card_creation.log',
|
|
# encoding='utf-8',
|
|
maxBytes=32 * 1024 * 1024, # 32 MiB
|
|
backupCount=5, # Rotate through 5 files
|
|
)
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
handler.setFormatter(formatter)
|
|
logger.addHandler(handler)
|
|
|
|
def log_exception(e: Exception, msg: str = '', level: Literal['debug', 'error', 'info', 'warn'] = 'error'):
|
|
if level == 'debug':
|
|
logger.debug(msg, stack_info=True)
|
|
elif level == 'error':
|
|
logger.error(msg, stack_info=True)
|
|
elif level == 'info':
|
|
logger.info(msg, stack_info=True)
|
|
else:
|
|
logger.warning(msg, stack_info=True)
|
|
raise e(msg) |