Advertisement
hackbyte

python_barebone_with_logging.py

Aug 27th, 2018
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.08 KB | None | 0 0
  1. #!/usr/bin/env python3.6
  2. # -*- coding: utf-8 -*-
  3. """
  4. $ python3(.6) python_barebone.py
  5.  
  6. This script will do the following things:
  7.  
  8. Load it's own path and check for an existing include/ dir in it,
  9. so you can simply load whatever is in there as import <modulename>.
  10.  
  11. Then it will set up python logging with some pretty verbose information.
  12. There are examples for Console, File and Syslog destinations.
  13.  
  14. After logging is set up, this script will parse it's command line
  15. using argparse and if it encounters a '-d' or '--debug' as parameter,
  16. it will enable full debug logging to all enabled/configured
  17. destinations (handlers).
  18.  
  19. have fun.. ;)
  20.  
  21. 2018-08-28 hackbyte (d. mitzlaff / pythonstuff at hackbyte period de)
  22.  
  23. I'm open for donations if you like, i'm just a poor life artist. ;)
  24.  bitcoin: 1bytoroRvXLGc1FWTob5h1oH92VMdUSFi
  25.  ethereum: 0x97cfdffb3bd4d77694891dabe701cff2d277dfbd
  26. Or just ask me .. google knows me. ;)
  27.  
  28. Oh well, i'm open for comments, critics and suggestions too,
  29. i'm pretty new with this python stuff. ;)
  30.  
  31. """
  32.  
  33. ################################################################################
  34. #
  35. # first imports
  36. #
  37. import os, sys, shutil, time, datetime, locale, asyncio, json
  38. import logging, logging.handlers, inspect, operator, argparse
  39.  
  40. ################################################################################
  41. #
  42. # save scriptpath
  43. #
  44. # from https://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing
  45. #  https://stackoverflow.com/questions/1296501/find-path-to-currently-running-file
  46. #  https://docs.python.org/3.6/library/inspect.html and
  47. #  https://docs.python.org/3.6/library/os.path.html
  48. #  https://stackoverflow.com/questions/8663076/python-best-way-to-add-to-sys-path-relative-to-the-current-running-script
  49. #
  50. scriptpath  = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  51. #
  52. # save our scriptname (basename of the full blown path like above)
  53. #
  54. scriptname  = os.path.basename(inspect.getfile(inspect.currentframe()))
  55.  
  56. #
  57. # (if) we want to include stuff from an include/ dir _in the path where the script lies in_!
  58. #
  59. #  from https://stackoverflow.com/questions/2349991/how-to-import-other-python-files
  60. #  https://stackoverflow.com/questions/8663076/python-best-way-to-add-to-sys-path-relative-to-the-current-running-script
  61. #  https://docs.python.org/3/library/importlib.html#module-importlib ...
  62. #
  63. if 'include' == os.path.basename(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))):
  64.     #
  65.     # save the dirname of the parent directory
  66.     #
  67.     scriptpath = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
  68. #
  69. # or else
  70. #
  71. else:
  72.     #
  73.     # save our dir
  74.     #
  75.     scriptpath  = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  76. #
  77. # set includepath als include within scriptpath dir
  78. #
  79. includepath = scriptpath + '/include'
  80. #
  81. # check if includepath exists
  82. #  see https://stackoverflow.com/questions/8933237/how-to-find-if-directory-exists-in-python
  83. #
  84. if os.path.isdir(includepath):
  85.     #
  86.     # if so, set it as additional import path
  87.     #
  88.     sys.path.insert(0, includepath)
  89. #
  90. # if not
  91. #
  92. else:
  93.     #
  94.     # complain!!!
  95.     #
  96.     print("No include path found!")
  97.     #
  98.     # and gtfo
  99.     #
  100.     sys.exit(23)
  101. #
  102. # pudb debugging...... if needed, you can anywhere in the source
  103. # activate the debugger with putting 'set_trace()' in a line
  104. #
  105. # see https://stackoverflow.com/questions/1623039/python-debugging-tips
  106. #  https://pypi.org/project/pudb/ and
  107. #  http://heather.cs.ucdavis.edu/~matloff/pudb.html
  108. #
  109. ##from pudb import set_trace
  110. #
  111.  
  112. ################################################################################
  113. #
  114. # Now We're sure to have any needed include path.... for reasons
  115. #
  116. # load helper stuff (from my own stuff ;))
  117. #
  118. ##import stuff_helper
  119. # or
  120. ##from stuff_helper import myfunc
  121. #
  122.  
  123. ################################################################################
  124. #
  125. # set up and configure python logging.
  126. #
  127. # see  https://docs.python.org/3.6/howto/logging.html
  128. #  https://docs.python.org/3.6/howto/logging-cookbook.html
  129. #  https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
  130. #
  131. # create log object
  132. #
  133. log = logging.getLogger()
  134.  
  135. #
  136. # by default, log all INFO messages. ;)
  137. #
  138. log.setLevel(logging.INFO)
  139.  
  140. #
  141. # Formatting for anything not syslog (so we need our own timestamp)
  142. # watch out for the '.%(msecs)03d' part, it is useful if your
  143. # script may output several messages within one second. ;)
  144. #
  145. # see: https://stackoverflow.com/questions/31328300/python-logging-module-logging-timestamp-to-include-microsecond
  146. #  https://stackoverflow.com/questions/6290739/python-logging-use-milliseconds-in-time-format
  147. #
  148. logformatter_main = logging.Formatter(
  149.     '%(asctime)s.%(msecs)03d ' +
  150.     '%(levelname)-8s ' +
  151.     '%(filename)-16s ' +
  152.     '%(funcName)-12s ' +
  153.     '%(lineno)04d ' +
  154.     '%(message)s',
  155.     '%Y%m%d%H%M%S')
  156. #
  157. # for syslog (so we do not need our own timestamp)
  158. #
  159. logformatter_syslog = logging.Formatter(
  160.     '%(levelname)-8s ' +
  161.     '%(filename)-16s ' +
  162.     '%(funcName)-12s ' +
  163.     '%(lineno)04d ' +
  164.     '%(message)s')
  165. #
  166. # create console log handler (stdout/stderr)
  167. #
  168. logcons = logging.StreamHandler()
  169. #
  170. # log even debug stuff to console (if enabled below;))
  171. #
  172. logcons.setLevel(logging.DEBUG)
  173. #
  174. # make it fancy
  175. #
  176. logcons.setFormatter(logformatter_main)
  177. #
  178. # and add handler to log..
  179. #
  180. log.addHandler(logcons)
  181.  
  182. #
  183. # create file handler which logs debug messages too
  184. #
  185. #logfile = logging.FileHandler(config['butler_home'] + '/allmessages.log')
  186. #logfile.setLevel(logging.DEBUG)
  187. #logfile.setFormatter(logformatter_main)
  188. #log.addHandler(logfile)
  189.  
  190. #
  191. # create a syslog handler, by default it will only log
  192. # INFO and more important messages...
  193. #
  194. #logsys = logging.handlers.SysLogHandler(address = '/dev/log')
  195. #logsys.setLevel(logging.INFO)
  196. #logsys.setFormatter(logformatter_syslog)
  197. #log.addHandler(logsys)
  198.  
  199. ################################################################################
  200. #
  201. # now, parse command line arguments...
  202. #
  203. # see: https://docs.python.org/3/library/argparse.html
  204. #  https://pymotw.com/2/argparse/
  205. #  https://docs.python.org/3/howto/argparse.html
  206. #
  207. # create parser object and give it a description.
  208. #
  209. parser = argparse.ArgumentParser(description=scriptname + ' commandline parameters.')
  210. #
  211. # add argument -c/--config for a configfile
  212. # default will be (our_path)/config/config.json
  213. #
  214. parser.add_argument(
  215.     "-c", "--config",
  216.     dest="config_file",
  217.     action="store",
  218.     default=scriptpath+"/config/config.json",
  219.     metavar="FILE",
  220.     help="Use configuration from FILE")
  221. #
  222. # add argument -d/--debug as simple boolean toggle,
  223. # defaults to false.
  224. #
  225. parser.add_argument(
  226.     "-d", "--debug",
  227.     dest="debug",
  228.     action="store_true",
  229.     default=False,
  230.     help="show heavy masses(!!) of debug output.")
  231. #
  232. # add argument -t/--test for test-run, defaults to false.
  233. #
  234. parser.add_argument(
  235.     "-t", "--test",
  236.     dest="test",
  237.     action="store_true",
  238.     default=False,
  239.     help="for testing environment")
  240. #
  241. # add an argument for some parameter (telegram api token in my case;)
  242. #
  243. parser.add_argument(
  244.     "-a", "--api",
  245.     dest="token",
  246.     action="store",
  247.     default="",
  248.     metavar="TELEGRAM_TOKEN",
  249.     help="Telegram bot api token to use")
  250. #
  251. # And add an argument for a owner id for telegram too
  252. #
  253. parser.add_argument(
  254.     "-o", "--owner",
  255.     dest="owner",
  256.     action="store",
  257.     default="",
  258.     metavar="UID",
  259.     help="Owner ID")
  260. #
  261. # mkay now the f.. parse that!! ;)
  262. #
  263. cmdline = parser.parse_args()
  264.  
  265. ################################################################################
  266. #
  267. # if debug parameter is set from commandline, make it so........
  268. #
  269. if cmdline.debug:
  270.     log.setLevel(logging.DEBUG)
  271.  
  272. ################################################################################
  273. #
  274. # we're alive!
  275. #
  276. log.debug("python version " + str(sys.version).replace('\n', ''))
  277. log.debug("program '"+scriptname+"' start " + '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
  278.  
  279. ################################################################################
  280. #
  281. # log what we got from commandline.....
  282. #
  283. log.debug("cmdline.config_file = "  + str(cmdline.config_file))
  284. log.debug("cmdline.debug = "        + str(cmdline.debug))
  285. log.debug("cmdline.test = "         + str(cmdline.test))
  286. log.debug("cmdline.token = "        + str(cmdline.token))
  287. log.debug("cmdline.owner = "        + str(cmdline.owner))
  288.  
  289.  
  290. ################################################################################
  291. #
  292. # yeah..........................................................................
  293. #
  294. # Here you can start your program, and use any log method from above.
  295. #
  296. # here come some examples.
  297.  
  298. log.debug("Well, this is a debug message!")
  299.  
  300. log.info("This is a simple info message. ;)")
  301.  
  302. log.warning("We also can WARN about things now!")
  303.  
  304. log.error("Even if we encounter some errors.")
  305.  
  306. log.critical("We even can tell the user if a critical problem occurs.")
  307.  
  308. # All done, clean exit. ;)
  309. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement