Skip to content

Instantly share code, notes, and snippets.

@stevexyz
Created May 7, 2018 20:58
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 stevexyz/f18457f6bce80ae16a76fd64a3485f15 to your computer and use it in GitHub Desktop.
Save stevexyz/f18457f6bce80ae16a76fd64a3485f15 to your computer and use it in GitHub Desktop.
Python debug template
# from: http://web.archive.org/web/20121122043920/http://aymanh.com:80/python-debugging-techniques
# By default, the logging module prints critical, error and warning messages. To change this so that all levels are printed, use:
# $ ./your-program.py --logging=debug
# To send log messages to a file called debug.log, use:
# $ ./your-program.py --logging-level=debug --logging-file=debug.log
import logging
import optparse
LOGGING_LEVELS = {'critical': logging.CRITICAL,
'error': logging.ERROR,
'warning': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG}
def main():
parser = optparse.OptionParser()
parser.add_option('-l', '--logging-level', help='Logging level')
parser.add_option('-f', '--logging-file', help='Logging file name')
(options, args) = parser.parse_args()
logging_level = LOGGING_LEVELS.get(options.logging_level, logging.NOTSET)
logging.basicConfig(level=logging_level, filename=options.logging_file,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Your program goes here.
# You can access command-line arguments using the args variable.
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment