set_logging

Function

def set_logging(filename='app', level='INFO', backup_count=7):
    if hasattr(logging, level.upper()):
        os.makedirs('./data/logs/', exist_ok=True)
        logging.basicConfig(
            format='%(asctime)s %(name)s %(levelname)s %(message)s',
            datefmt='%H:%M:%S',
            level=getattr(logging, level.upper()),
            handlers=[
                TimedRotatingFileHandler(
                    "./data/logs/{}.log".format(filename),
                    when="midnight",
                    interval=1,
                    backupCount=backup_count
                ),
                logging.StreamHandler(sys.stdout)
            ]
        )
        return True
    raise Exception("Invalid logging level")

Description

  • Check if the specified logging level exists for the logging module

  • Raise an exception if logging level doesn’t exist

  • Create the logs folder if it doesn’t exist

  • Set the logging configurations with specified filename or default to “app.log”

  • Return True