Skip to content

Instantly share code, notes, and snippets.

@abhiomkar
Last active December 30, 2020 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abhiomkar/6449478 to your computer and use it in GitHub Desktop.
Save abhiomkar/6449478 to your computer and use it in GitHub Desktop.
Python Logging Cheatsheet
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