Last active
December 30, 2020 21:10
-
-
Save abhiomkar/6449478 to your computer and use it in GitHub Desktop.
Python Logging Cheatsheet
This file contains hidden or 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 logging | |
# prints log to stdout and also saves to specified log file | |
logger = logging.getLogger('my_logfile') | |
fh = logging.FileHandler('my_logfile.log') | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
fh.setFormatter(formatter) | |
ch = logging.StreamHandler() | |
ch.setFormatter(formatter) | |
logger.addHandler(fh) | |
logger.addHandler(ch) | |
logger.setLevel(logging.DEBUG) | |
logger.info("This is information.") | |
logger.error("This is Error!") | |
logger.debug("This is debug...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment