Created
October 5, 2016 18:57
-
-
Save mr-rigden/3dce5c9a2cdf7f4aa43b4a2ce5e036f7 to your computer and use it in GitHub Desktop.
Mr. Rigden's Python Logging Boilerplate: Now with Color
This file contains hidden or 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 | |
| import coloredlogs | |
| LOG_FORMAT = '%(asctime)s %(levelname)-8s %(message)s' | |
| LOG_DATE = "%Y-%m-%d %H:%M:%S" | |
| LOG_STYLE = dict( | |
| debug=dict(color='cyan', bold=True), | |
| info=dict(color='green', bold=True), | |
| verbose=dict(color='white'), | |
| warning=dict(color='yellow', bold=True), | |
| error=dict(color='red', bold=True), | |
| critical=dict(color='magenta', bold=True)) | |
| logger = logging.getLogger('logging_template') | |
| logger.setLevel(logging.DEBUG) | |
| formatter = logging.Formatter(LOG_FORMAT, LOG_DATE) | |
| fileHander = logging.FileHandler('log.log') | |
| fileHander.setLevel(logging.DEBUG) | |
| fileHander.setFormatter(formatter) | |
| logger.addHandler(fileHander) | |
| coloredlogs.install(level='DEBUG', fmt=LOG_FORMAT, datefmt=LOG_DATE, level_styles=LOG_STYLE) | |
| # Some examples. | |
| logger.debug("this is a debugging message") | |
| logger.info("this is an informational message") | |
| logger.warn("this is a warning message") | |
| logger.error("this is an error message") | |
| logger.critical("this is a critical message") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment