Created
August 5, 2013 07:32
-
-
Save ysheng26/6154074 to your computer and use it in GitHub Desktop.
Convenient template for logging
(https://gimmebar-assets.s3.amazonaws.com/4fe38b76be0a5.html)
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 | |
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