import sys
import logging.handlers

from ..configs import config_paths

ERROR_LOG = config_paths() / 'errors.log'
# initialize logger stuff
FORMAT = '%(asctime)s - %(levelname)s - %(name)s : %(message)s'
DATEFMT = '%d/%m/%Y %H:%M:%S'
LEVEL = logging.INFO
myformatter = logging.Formatter(fmt=FORMAT, datefmt=DATEFMT)

logging.basicConfig(level=LEVEL, format=FORMAT, datefmt=DATEFMT)
flogger = logging.handlers.RotatingFileHandler(str(ERROR_LOG), maxBytes=1024*1024*10, backupCount=2)
flogger.setFormatter(myformatter)
flogger.setLevel(LEVEL)

logger = logging.getLogger()
logger.addHandler(flogger)
logger.addHandler(logging.NullHandler())


# log uncaught errors with traceback, ignore KeyboardInterrupz
def handle_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return
    logger.error("Uncaught exception:", exc_info=(exc_type, exc_value, exc_traceback))
    return
