Created
October 12, 2018 05:17
-
-
Save GenevieveBuckley/189beb45c5d7333f4a134dc9e493c9bf to your computer and use it in GitHub Desktop.
Logging in python - setting up output to file and terminal simultaneously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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