Skip to content

Instantly share code, notes, and snippets.

@tldrafael
Last active May 13, 2022 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tldrafael/7f5cffd67e3cb4de8a64b639d8d91b74 to your computer and use it in GitHub Desktop.
Save tldrafael/7f5cffd67e3cb4de8a64b639d8d91b74 to your computer and use it in GitHub Desktop.
Basic python logging configuration
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