''' Created on 2 nov 2019 @author: Emanuele Trabattoni ''' import sys import json import logging import colorama from PyQt5.QtCore import QObject, pyqtSignal class fancyLogger(QObject): ''' Colorizza il logger di python, per un' esperienza stile willy wonka ''' sendLog = pyqtSignal(str) def __init__(self, filepath=None, name="Logger", consoleLog=True, fileLog=False): QObject.__init__(self) try: with open(filepath, 'r') as fp: settings = json.load(fp) fp.close() colorama.init(convert=True) self.LRED = colorama.Fore.LIGHTRED_EX self.RED = colorama.Fore.RED self.LYELLOW = colorama.Fore.LIGHTYELLOW_EX self.YELLOW = colorama.Fore.YELLOW self.LBLUE = colorama.Fore.LIGHTBLUE_EX self.BLUE = colorama.Fore.BLUE self.LGREEN = colorama.Fore.LIGHTGREEN_EX self.LGREEN = colorama.Fore.GREEN self.WHITE = colorama.Fore.LIGHTWHITE_EX self.RST = colorama.Style.RESET_ALL # Setup Logger self.LOGGER = logging.getLogger(name) self.LOGGER.setLevel(logging.DEBUG) self.LOGGER.propagate = False FORMATTER = logging.Formatter((settings["logFormat"]), (settings["logTimeFormat"])) if fileLog: # File Logging fh = logging.FileHandler((settings["logFile"])) fh.setLevel(logging.DEBUG) fh.setFormatter(FORMATTER) self.LOGGER.addHandler(fh) if consoleLog: # Console Logging cl= logging.StreamHandler(sys.stdout) cl.setLevel(logging.DEBUG) cl.setFormatter(FORMATTER) self.LOGGER.addHandler(cl) except IOError as e: print("Impossibile caricare la configurazione del logger: [{}]".format(e)) except Exception as ee: print(f"libFancylogger error: {e}") pass def debug(self, msg="Undefined Debug"): print(self.LBLUE, end='') self.LOGGER.debug(msg) print(self.RST, end='') self.sendLog.emit(f"DEBUG | {msg}
") pass def info(self, msg="Undefined Info"): print(self.LGREEN, end='') self.LOGGER.info(msg) print(self.RST, end='') self.sendLog.emit(f"INFO | {msg}
") pass def warn(self, msg="Undefined Warning"): print(self.LYELLOW, end='') self.LOGGER.warning(msg) print(self.RST, end='') self.sendLog.emit(f"WARNING | {msg}
") pass def error(self, msg="Undefined Error"): print(self.LRED, end='') self.LOGGER.error(msg) print(self.RST, end='') self.sendLog.emit(f"ERROR | {msg}
") pass def critical(self, msg="Undefined Critical"): print(self.RED, end='') self.LOGGER.critical(msg) print(self.RST, end='') self.sendLog.emit(f"CRITICAL | {msg}
") pass def testColors(self): self.debug("Test Debug") self.info("Test Info") self.warn("Test Warning") self.error("Test Error") self.critical("Test Critical") pass