Skip to content

Instantly share code, notes, and snippets.

@yang-zhang
Last active January 26, 2020 01:02
Embed
What would you like to do?
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