Last active
May 13, 2022 17:56
-
-
Save tldrafael/7f5cffd67e3cb4de8a64b639d8d91b74 to your computer and use it in GitHub Desktop.
Basic python logging configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from logging.handlers import RotatingFileHandler | |
import traceback | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
handler = RotatingFileHandler('app.log', mode='a', maxBytes=50e6, backupCount=2) | |
formatter = logging.Formatter('%(asctime)s,%(message)s', datefmt='%Y-%m-%d %H:%M:%S') | |
handler.setFormatter(formatter) | |
handler.setLevel(logging.DEBUG) | |
logger.addHandler(handler) | |
# Example to capture generic exceptions | |
try: | |
logger.info('{},{}'.format(impath, 'Starting to process')) | |
... | |
logger.info('{},{}'.format(impath, 'Face processed')) | |
except Exception as e: | |
traceback_info = ''.join(traceback.format_exception(*sys.exc_info())) | |
logger.error('{},{},{},{}'.format('', '', '', 'ERROR - {} - {}'.format(e, traceback_info))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment