Last active
January 26, 2020 01:02
-
-
Save yang-zhang/66f0f7153d6d168f9e232d9a027f88eb to your computer and use it in GitHub Desktop.
logging template
This file contains 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 | |
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