Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Last active September 4, 2017 07:42
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 gamesbook/bf06025d5f6a6e9af6ccf968f167b746 to your computer and use it in GitHub Desktop.
Save gamesbook/bf06025d5f6a6e9af6ccf968f167b746 to your computer and use it in GitHub Desktop.
Demonstrate use of Python's basic log operations
# -*- coding: utf-8 -*-
""" Purpose: Demonstrate use of Python's basic log operations
Created: 2017-09-01
Contact: dhohls@csir.co.za
Usages::
python logging_demo.py
python logging_demo.py -ll=ERROR
"""
__author__ = "Derek Hohls"
__version__ = "0.1.1"
from __future__ import print_function
import argparse
import logging
import sys
log = logging.getLogger(__name__)
LOG_LEVEL = 'INFO'
class LogTest(object):
def test(self):
""""Test use of log."""
log.critical('test critical')
log.error('test error')
log.warn('test warn')
log.info('test info')
log.debug('test debug')
def main(args):
"""Process args."""
print(args)
logging.basicConfig(
stream=sys.stdout,
format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d %(funcName)s()] %(message)s',
level=getattr(logging, args.loglevel))
# test
logging.critical('critical')
logging.error('error')
logging.warn('warn')
logging.info('info')
logging.debug('debug')
print('logging.DEBUG =', logging.DEBUG)
# extra test
LogTest().test()
if __name__ == '__main__':
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'-ll', '--loglevel', default=LOG_LEVEL,
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Set log level for service (%s)" % LOG_LEVEL)
ARGS = PARSER.parse_args()
LOG_LEVEL = ARGS.loglevel
main(ARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment