Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yang-zhang
Last active January 26, 2020 01:02
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 yang-zhang/66f0f7153d6d168f9e232d9a027f88eb to your computer and use it in GitHub Desktop.
Save yang-zhang/66f0f7153d6d168f9e232d9a027f88eb to your computer and use it in GitHub Desktop.
logging template
import logging
log = logging.getLogger(__name__)
log.setLevel(level=logging.DEBUG)
fmtr = logging.Formatter("[%(asctime)s] [%(levelname)-8.8s] %(message)s", "%Y-%m-%d %H:%M:%S")
fhdlr = logging.FileHandler(f"log.log", mode='w')
fhdlr.setFormatter(fmtr)
log.addHandler(fhdlr)
chdlr = logging.StreamHandler()
chdlr.setFormatter(fmtr)
log.addHandler(chdlr)
log.debug('This is a debug message')
log.info('This is an info message')
log.warning('This is a warning message')
log.error('This is an error message')
log.critical('This is a critical message')
# [2020-01-25 19:54:40] [DEBUG ] This is a debug message
# [2020-01-25 19:54:40] [INFO ] This is an info message
# [2020-01-25 19:54:40] [WARNING ] This is a warning message
# [2020-01-25 19:54:40] [ERROR ] This is an error message
# [2020-01-25 19:54:40] [CRITICAL] This is a critical message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment