From e21efe875c6ec71603e745fe4f798955abfe2abd Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 6 Jul 2022 11:20:23 +0800 Subject: [PATCH] feat: global logging module --- Basis/Logger.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Basis/Logger.py diff --git a/Basis/Logger.py b/Basis/Logger.py new file mode 100644 index 0000000..383cee2 --- /dev/null +++ b/Basis/Logger.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys +import logging +from colorlog import ColoredFormatter + +logFile = 'runtime.log' +logLevel = logging.DEBUG +# logLevel = logging.WARNING +dateFormat = '%Y-%m-%d %H:%M:%S' +logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)' +logging.basicConfig( + level = logLevel, + format = logFormat, + datefmt = dateFormat, + filename = logFile, +) +logHandler = logging.StreamHandler(stream = sys.stdout) +logHandler.setFormatter(ColoredFormatter( + fmt = '%(log_color)s' + logFormat, + datefmt = dateFormat, + log_colors = { + 'DEBUG': 'white', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'bold_red', + } +)) +logging.getLogger().addHandler(logHandler) + + +if __name__ == '__main__': + logging.debug('debug') + logging.info('info') + logging.warning('warn') + logging.error('error') + logging.critical('critical')