Skip to content

Instantly share code, notes, and snippets.

@alexsavio
Forked from abhiomkar/python_logging.py
Last active September 12, 2015 10:53
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 alexsavio/c5c43dfd026572b74e04 to your computer and use it in GitHub Desktop.
Save alexsavio/c5c43dfd026572b74e04 to your computer and use it in GitHub Desktop.
Python Logging Cheatsheet
import logging
# prints to stdout
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT)
log = logging.getLogger(__file__)
log.setLevel(logging.DEBUG)
# prints log to stdout and also saves to specified log file
log = 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)
log.addHandler(fh)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
log.info("This is information.")
log.error("This is Error!")
log.debug("This is debug...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment