Skip to content

Instantly share code, notes, and snippets.

@DeastinY
Created November 14, 2019 14:06
Show Gist options
  • Save DeastinY/e27df5e85f6251d922c70da4edcdd5a4 to your computer and use it in GitHub Desktop.
Save DeastinY/e27df5e85f6251d922c70da4edcdd5a4 to your computer and use it in GitHub Desktop.
Example for python logging
#!/usr/bin/python3
import logging
# log levels are debug, info, warning, error and critical. Setting e.g. info logs info and above (warning, error, critical)
# logging.basicConfig(level=logging.INFO, filename="logging_sample.log")
# Basic logging is probably not sufficient. You can and should create your own Logger like this
logger = logging.getLogger('logging_sample') # Use this name to get the same logger from different submodules
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)5s() ] %(message)s"
logging.basicConfig(format=FORMAT, filename="logging_sample.log", filemode="w") # default for filemode is append, w = (over)write
logger.setLevel(logging.INFO)
def raw_reading(data):
logger.info(f"{data}")
# create and save a image as well
image_filename = "7589346340.png"
logger.info(f"![raw_reading Image]({image_filename})") # use e.g. Markdown Syntax here
return data + [":o"]
if __name__ == '__main__':
# All documentation is at https://docs.python.org/3/library/logging.html
logger.debug("This won't be logged, as it's level DEBUG")
logger.info("# Report Start")
data = [1, 2, "test", [c for c in "Hallo"]]
raw_reading(data)
logger.info("# Report End")
# Now you could convert the Report to html or something https://github.com/trentm/python-markdown2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment