Last active
December 28, 2015 22:47
-
-
Save lx-88/fb940e8ad04c0342937a to your computer and use it in GitHub Desktop.
Demonstrate a quick example of how to use argparse and to setup python logging (both console and to a file) and pass the logger between functions.
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 | |
""" | |
MIT License (MIT) | |
Copyright (c) 2015. Michael Ewald <[email protected]>, GeomaticsResearch LLC | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import logging | |
def hello_world_worker(msg, logger=None): | |
"""Say hello world and demonstrate passing a logger around. | |
:param msg: The message to say to the user | |
:param logger: A logger object. | |
:return: The msg | |
""" | |
theLogger = logger or logging.getLogger(__name__) | |
theLogger.info(msg) | |
return msg | |
def hello_world(msg, logger=None): | |
"""Say hello world using a child function of this function and demonstrate passing a logger around. | |
:param msg: The message to say to the user | |
:param logger: A logger object. | |
:return: The msg | |
""" | |
theLogger = logger or logging.getLogger(__name__) | |
func_result = hello_world_worker(msg, logger=theLogger) | |
return func_result | |
if __name__ == "__main__": | |
"""Run the script from the command line using argparse | |
""" | |
import argparse | |
parser = argparse.ArgumentParser(description="Demonstrate how to use argparse and pass a logger around functions") | |
parser.add_argument("msg", type=str, help="The message you want to say to the user") | |
parser.add_argument("-log", type=str, dest='logfile', help='The filepath of the logfile to write to (Default: None)', default=None) | |
args = parser.parse_args() | |
# Set up the logger | |
logger = logging.getLogger(__file__) # Create the logger | |
logger.setLevel(logging.DEBUG) # Set log level to DEBUG | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # create formatter | |
if args.logfile: | |
flh = logging.FileHandler(args.logfile) # create file console logger | |
flh.setLevel(logging.DEBUG) | |
flh.setFormatter(formatter) | |
logger.addHandler(flh) | |
else: | |
ch = logging.StreamHandler() # create console handler | |
ch.setLevel(logging.DEBUG) # set level to debug | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
# Run the function | |
hello_world(args.msg, logger=logger) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment