Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Created October 12, 2018 05:17
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 GenevieveBuckley/189beb45c5d7333f4a134dc9e493c9bf to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/189beb45c5d7333f4a134dc9e493c9bf to your computer and use it in GitHub Desktop.
Logging in python - setting up output to file and terminal simultaneously
import time
import logging
# See the docs: https://docs.python.org/3/library/logging.html
timestamp = time.strftime('%d-%b-%Y_%H-%M%p', time.localtime())
log_filename = "path/to/log.txt"
logging.basicConfig(
format="%(asctime)s %(message)s",
level=logging.DEBUG,
handlers=[
logging.FileHandler(f"{log_filename}.log"),
logging.StreamHandler()
])
# There are different levels of logging
# Adjust level=logging.DEBUG to level=logging.ERROR and see what happens
logging.debug("Debugging information logged.")
logging.info("Useful information logged.")
logging.warning("Warning message logged.")
logging.error("Error message logged.")
logging.critical("Critical error message logged.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment