Skip to content

Instantly share code, notes, and snippets.

@aliabbasjp
Created February 22, 2017 14:10
Show Gist options
  • Save aliabbasjp/42a309b5373feb1828af683943b07e4b to your computer and use it in GitHub Desktop.
Save aliabbasjp/42a309b5373feb1828af683943b07e4b to your computer and use it in GitHub Desktop.
A template to begin Python Programs
#coding=utf8
"""
SYNOPSIS
TODO helloworld [-h,--help] [-v,--verbose] [--version]
DESCRIPTION
TODO This describes how to use this script. This docstring
will be printed by the script if there is an error or
if the user requests help (-h or --help).
EXAMPLES
TODO: Show some examples of how to use this script.
EXIT STATUS
TODO: List exit codes
AUTHOR
Aliabbas Petiwala <[email protected]>
currenttime
LICENSE
Confidential
VERSION
$
TODO:
"""
import sys, os, traceback, optparse
import time
import re
#from pexpect import run, spawn
import logging
def get_logger(name=None):
default = "__app__"
formatter = logging.Formatter('%(levelname)s: %(asctime)s %(funcName)s(%(lineno)d) -- %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
log_map = {"__app__": "app.log", "__basic_log__": "file1.log", "__advance_log__": "file2.log"}
if name:
logger = logging.getLogger(name)
else:
logger = logging.getLogger(default)
fh = logging.FileHandler(log_map[name])
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.setLevel(logging.DEBUG)
return logger
def main ():
global options, args
import logging.config
# logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# logging.config.fileConfig('logging.conf',disable_existing_loggers=False)
# TODO: Do something more interesting here...
a=get_logger("__app___")
b=get_logger("__basic_log__")
a.info("Starting logging!")
b.debug("Debug Mode")
print 'Hello world!'
if __name__ == '__main__':
try:
start_time = time.time()
parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='')
parser.add_option ('-v', '--verbose', action='store_true', default=False, help='verbose output')
(options, args) = parser.parse_args()
# if len(args) < 1:
# parser.error ('missing argument')
if options.verbose: print time.asctime()
main()
if options.verbose: print time.asctime()
if options.verbose: print 'TOTAL TIME IN MINUTES:',
if options.verbose: print (time.time() - start_time) / 60.0
sys.exit(0)
except KeyboardInterrupt, e: # Ctrl-C
raise e
except SystemExit, e: # sys.exit()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment