From 12501c4edc6787aeca04c4f973baebfaf45e6d76 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Wed, 18 Dec 2019 19:35:24 +0100 Subject: [PATCH] ripensamenti sui file di configurazione, il logger legge un file separato --- bananaSPLIT/libbananasplit/libfancylogger.py | 70 +++++++++++--------- bananaSPLIT/libbananasplit/loggerconf.json | 5 ++ bananaSPLIT/libbananasplit/testEN.json | 5 -- 3 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 bananaSPLIT/libbananasplit/loggerconf.json diff --git a/bananaSPLIT/libbananasplit/libfancylogger.py b/bananaSPLIT/libbananasplit/libfancylogger.py index b6420fd..1e0fb1f 100644 --- a/bananaSPLIT/libbananasplit/libfancylogger.py +++ b/bananaSPLIT/libbananasplit/libfancylogger.py @@ -3,46 +3,52 @@ Created on 2 nov 2019 @author: Emanuele Trabattoni ''' -import sys, os +import sys import json import logging import colorama +from bananaSPLIT.main import fp class fancyLogger(object): ''' Colorizza il logger di python, per un' esperienza stile willy wonka ''' - def __init__(self, name="Logger", consoleLog=True, fileLog=True): - settings = json.load(open(os.getcwd()+r"\libbananasplit\testEN.json"))["logger"] - 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) + def __init__(self, filepath=None, name="Logger", consoleLog=True, fileLog=False): + 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)) pass def debug(self, msg="Undefined Debug"): diff --git a/bananaSPLIT/libbananasplit/loggerconf.json b/bananaSPLIT/libbananasplit/loggerconf.json new file mode 100644 index 0000000..51b10ab --- /dev/null +++ b/bananaSPLIT/libbananasplit/loggerconf.json @@ -0,0 +1,5 @@ +{ + "logFile": "D:\\Test\\bananaSPLIT.log", + "logFormat": "%(asctime)s|%(levelname)-8s| %(message)-50s", + "logTimeFormat": "%m-%d %H:%M:%S" +} diff --git a/bananaSPLIT/libbananasplit/testEN.json b/bananaSPLIT/libbananasplit/testEN.json index 8607e50..f7e68d5 100644 --- a/bananaSPLIT/libbananasplit/testEN.json +++ b/bananaSPLIT/libbananasplit/testEN.json @@ -1,10 +1,5 @@ { "version": "v1.1a", - "logger": { - "logFile": "D:\\Test\\bananaSPLIT.log", - "logFormat": "%(asctime)s|%(levelname)-8s| %(message)-50s", - "logTimeFormat": "%m-%d %H:%M:%S" - }, "splitter": { "name": "", "paths": {