Created
October 16, 2018 11:13
-
-
Save vsajip/3e005bd26891fde319837f5211a532b1 to your computer and use it in GitHub Desktop.
CLI starter template for Python logging cookbook
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 argparse | |
import importlib | |
import logging | |
import os | |
import sys | |
def main(args=None): | |
scriptname = os.path.basename(__file__) | |
parser = argparse.ArgumentParser(scriptname) | |
levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') | |
parser.add_argument('--log-level', default='INFO', choices=levels) | |
subparsers = parser.add_subparsers(dest='command', | |
help='Available commands:') | |
start_cmd = subparsers.add_parser('start', help='Start a service') | |
start_cmd.add_argument('name', metavar='NAME', | |
help='Name of service to start') | |
stop_cmd = subparsers.add_parser('stop', | |
help='Stop one or more services') | |
stop_cmd.add_argument('names', metavar='NAME', nargs='+', | |
help='Name of service to stop') | |
restart_cmd = subparsers.add_parser('restart', | |
help='Restart one or more services') | |
restart_cmd.add_argument('names', metavar='NAME', nargs='+', | |
help='Name of service to restart') | |
options = parser.parse_args() | |
# the code to dispatch commands could all be in this file. For the purposes | |
# of illustration only, we implement each command in a separate module. | |
try: | |
mod = importlib.import_module(options.command) | |
cmd = getattr(mod, 'command') | |
except (ImportError, AttributeError): | |
print('Unable to find the code for command \'%s\'' % options.command) | |
return 1 | |
# Could get fancy here and load configuration from file or dictionary | |
logging.basicConfig(level=options.log_level, | |
format='%(levelname)s %(name)s %(message)s') | |
cmd(options) | |
if __name__ == '__main__': | |
sys.exit(main()) |
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 | |
logger = logging.getLogger(__name__) | |
def command(options): | |
n = len(options.names) | |
if n == 1: | |
plural = '' | |
services = '\'%s\'' % options.names[0] | |
else: | |
plural = 's' | |
services = ', '.join('\'%s\'' % name for name in options.names) | |
i = services.rfind(', ') | |
services = services[:i] + ' and ' + services[i + 2:] | |
logger.debug('About to restart %s', services) | |
# actually do the command processing here ... | |
logger.info('Restarted the %s service%s.', services, plural) |
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 | |
logger = logging.getLogger(__name__) | |
def command(options): | |
logger.debug('About to start %s', options.name) | |
# actually do the command processing here ... | |
logger.info('Started the \'%s\' service.', options.name) |
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 | |
logger = logging.getLogger(__name__) | |
def command(options): | |
n = len(options.names) | |
if n == 1: | |
plural = '' | |
services = '\'%s\'' % options.names[0] | |
else: | |
plural = 's' | |
services = ', '.join('\'%s\'' % name for name in options.names) | |
i = services.rfind(', ') | |
services = services[:i] + ' and ' + services[i + 2:] | |
logger.debug('About to stop %s', services) | |
# actually do the command processing here ... | |
logger.info('Stopped the %s service%s.', services, plural) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment