FancyLogger funzionante e file di configurazione test

This commit is contained in:
2019-11-02 14:29:48 +01:00
parent 0502d77c14
commit dfcbef7ae4
2 changed files with 69 additions and 9 deletions

View File

@@ -3,30 +3,82 @@ Created on 2 nov 2019
@author: Emanuele Trabattoni
'''
import sys
import json
import logging
import colorama
class fancyLogger(object):
'''
Colorizza il logger di python, per un' esperienza alla willy wonka
'''
def __init__(self, name="Logger", consoleLog=True, fileLog=True, filePath=None):
settings = json.load(open("./testSettings.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
def __init__(self, params):
# 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)
pass
def debug(self):
def debug(self, msg="Undefined Debug"):
print(self.LBLUE, end='')
self.LOGGER.debug(msg+self.RST)
pass
def info(self):
def info(self, msg="Undefined Info"):
print(self.WHITE, end='')
self.LOGGER.info(msg+self.RST)
pass
def warn(self):
def warn(self, msg="Undefined Warning"):
print(self.LYELLOW, end='')
self.LOGGER.warning(msg+self.RST)
pass
def error(self):
def error(self, msg="Undefined Error"):
print(self.LRED, end='')
self.LOGGER.error(msg+self.RST)
pass
def critical(self):
def critical(self, msg="Undefined Critical"):
print(self.RED, end='')
self.LOGGER.critical(msg+self.RST)
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
if __name__ == "__main__":
l=fancyLogger(name="testLogger")
l.testColors()
pass

View File

@@ -0,0 +1,8 @@
{
"version": "v1.1a",
"logger": {
"logFile": "D:\\Test\\bananaSPLIT.log",
"logFormat": "%(asctime)s|%(levelname)-8s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
"logTimeFormat": "%m-%d %H:%M:%S"
}
}