Skip to content

Instantly share code, notes, and snippets.

@dale-c-anderson
Created November 8, 2023 00:55
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 dale-c-anderson/8a69e7fc807131c74c837ba3b2f36e9d to your computer and use it in GitHub Desktop.
Save dale-c-anderson/8a69e7fc807131c74c837ba3b2f36e9d to your computer and use it in GitHub Desktop.
Python 3 Boilerplate
"""
Document Description
"""
__author__ = "Anonymous Coward"
__version__ = "0.1.0"
__license__ = "GPLv3"
import argparse
# from logging.handlers import SysLogHandler
import logging
import sys
import re
# import datetime
# import subprocess
# import boto3
# import re
# from functools import lru_cache
def main():
log.debug(["args", args])
log.info(f'Starting')
log.debug("Debug is only shown in terminal if you use -vv")
log.info("Info is only shown in terminal if you use -v")
log.warning('This is a warning, which is shown in terminal by default')
log.error("This is an error, which is shown in terminal by default")
log.critical("This is a critical error, which is shown in terminal by default")
if __name__ == "__main__":
""" This is executed when run from the command line """
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="Verbosity (-v, -vv, etc)")
args = parser.parse_args()
# Let terminal operator control the logging level sent to stderr
levels = [
logging.WARNING,
logging.INFO,
logging.DEBUG,
]
level = levels[min(args.verbose, len(levels) - 1)]
##################################
# DO NOT SET basicConfig.
# Since we have handlers with individual log levels, using basicConfig will ruin everything.
###################################
# logging.basicConfig()
# datefmt='%Y-%m-%d %H:%M:%S',
# )
default_date_format = '%Y-%m-%d %H:%M:%S'
default_log_format = '%(asctime)s.%(msecs)03d %(levelname)s %(module)s/%(funcName)s(): %(message)s'
# Create our own custom logging configuration. Don't use the built-in automagic stuff.
log = logging.getLogger("foo")
# Set the minimum threshold needed by any handler, then override desired levels for each handler.
# If this is set to "CRITICAL", for instance, nothing will ever appear in any log handler.
log.setLevel(logging.DEBUG)
# Set up handler for terminal logging
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(level) # Let terminal log level be controlled by args
console_handler.setFormatter(logging.Formatter(default_log_format, datefmt=default_date_format))
log.addHandler(console_handler)
# # Set up handler for file logging
# file_handler = logging.FileHandler(re.sub(r'\.py$', '', __file__) + '.log')
# file_handler.setLevel(logging.DEBUG) # Log everything to file
# file_handler.setFormatter(logging.Formatter(default_log_format, datefmt=default_date_format))
# log.addHandler(file_handler)
# # Set up handler for syslog
# syslog_handler = SysLogHandler('/dev/log')
# syslog_handler.setLevel(logging.WARNING) # Log warnings or worse to syslog
# syslog_handler.setFormatter(logging.Formatter('%(module)s.%(funcName)s[%(process)d] %(levelname)s: %(message)s')) # Syslog already timestamps everything
# log.addHandler(syslog_handler)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment