Last active
January 18, 2019 14:23
-
-
Save pfuntner/b4f9ac34a1c587383c528150959e9cda to your computer and use it in GitHub Desktop.
Example of using Python logging
This file contains 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
#! /usr/bin/env python | |
import logging | |
import argparse | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('-v', '--verbose', dest='verbose', action='count', help='Enable debugging - multiple uses prints more messages') | |
group.add_argument('--loglevel', dest='loglevel', action='store', help='Set log level: DEBUG, INFO, WARNING, ERROR, CRITICAL') | |
args = parser.parse_args() | |
logging.basicConfig(format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s') | |
log = logging.getLogger() | |
log.setLevel(args.loglevel or (logging.WARNING - (args.verbose or 0) * 10)) | |
log.debug('This is a debugging message') | |
log.info('This is an informational message') | |
log.warn('This is a warning message') | |
log.error('This is an error message') | |
log.fatal('This is a fatal/critical message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment