Browse Source

logging of lists and dicts

refactor
Ulrich 2 years ago
parent
commit
1d7cdfddac
  1. 99
      basic/message.py
  2. 41
      test/test_11message.py
  3. 0
      tools/job_const.py

99
basic/message.py

@ -207,7 +207,7 @@ class Message:
def getLogLevel(self, tool="", comp=None):
"""
gets the decreasing level depending on tool and component
gets the increasing level depending on tool and component
if these arguments matches with a job-parameter the level decreases
:param tool:
:param comp:
@ -244,8 +244,13 @@ class Message:
else:
return False
def getFinalReturncode(self):
RETURN_TEXT = ["OK", "WARN", "ERROR", "FATAL"]
return RETURN_TEXT[self.rc]
def getFinalRc(self):
return int(int(self.rc))
RETURN_TEXT = ["OK", "WARN", "ERROR", "FATAL"]
return int(self.rc)
def setFatal(self, text):
""" Routine zum Setzen des RC und gleichzeitigem Schreiben des Logs """
@ -365,33 +370,86 @@ class Message:
self.debug(LIMIT_XTRACE, prio, text)
def getLoggingArgs(self, mlevel, prio, text):
verify = self.getLogLevel("msg_tool")
self.logTrace(verify, "getLoggingArgs: " + str(mlevel)+ ", "+self.format2Str(prio)+", "+self.format2Str(text))
out = {}
prefix = ""
if isinstance(mlevel, int):
out["mlevel"] = mlevel
else:
raise Exception("argument mlevel is not int "+str(mlevel))
a = text.split(":")
cat = ""
txt = ""
if len(a[0]) > 1 and a[0].lower() in LIST_MTEXT:
cat = a[0]
if len(a) > 1 and len(a[1]) > 1:
txt = a[1]
elif len(a[0]) > 1 and a[0].lower() not in LIST_MTEXT:
txt = a[0]
if isinstance(prio, int) and len(txt) > 1:
out["mprio"] = prio
elif len(txt) < 1 and isinstance(prio, str):
if len(text) < 1:
out["mprio"] = 0
if isinstance(prio, int):
txt = str(prio)
elif isinstance(prio, dict):
txt = self.formatDict2Str(prio)
elif isinstance(prio, str):
txt = prio
elif isinstance(prio, list):
txt = self.formatList2Str(prio)
else:
txt = str(prio)
else:
if isinstance(prio, int):
out["mprio"] = prio
else:
out["mprio"] = 0
if len(txt) < 1:
raise Exception("argument text is not fount " + str(mlevel) + ", " + str(prio) + ", " + str(text))
if len(cat) > 1:
out["mtext"] = cat + ": " + txt
prefix = text
text = prio
if isinstance(text, dict):
txt = self.formatDict2Str(text)
elif isinstance(text, str):
txt = text
elif isinstance(text, list):
txt = self.formatList2Str(text)
else:
txt = str(text)
if len(prefix) > 1:
if ":" in prefix:
out["mtext"] = prefix.strip() + " " + txt.strip()
else:
out["mtext"] = prefix.strip() + ": " + txt.strip()
else:
out["mtext"] = txt
return out
def format2Str(self, elem):
if isinstance(elem, dict):
return self.formatDict2Str(elem)
elif isinstance(elem, str):
return elem
elif isinstance(elem, list):
return self.formatList2Str(elem)
else:
return str(elem)
def formatDict2Str(self, args):
txt = "{"
for k in args:
if isinstance(args[k], dict):
txt += k + ": " + self.formatDict2Str(args[k]) + ", "
elif isinstance(args[k], str):
txt += k + ": " + args[k] + ", "
elif isinstance(args[k], list):
txt += k + ": " + self.formatList2Str(args[k]) + ", "
else:
txt += k + ": " + str(args[k]) + ", "
return txt[0:-2] + "}"
def formatList2Str(self, args):
txt = "["
for k in args:
if isinstance(k, dict):
txt += k + ": {" + self.formatDict2Str(k) + "}, "
elif isinstance(k, str):
txt += k + ", "
elif isinstance(k, list):
txt += self.formatList2Str(k) + ", "
else:
txt += str(k) + ", "
return txt[0:-2] + "]"
def log(self, mlevel, prio, text):
args = self.getLoggingArgs(mlevel, prio, text)
""" eigentliche Schreibroutine: hierin wird debug-Level beruecksichtgigt"""
@ -406,8 +464,11 @@ class Message:
self.messages.append(text)
def debug(self, mlevel, prio, text=""):
verify = self.getLogLevel("msg_tool")
args = self.getLoggingArgs(mlevel, prio, text)
if (args["mlevel"] + args["mprio"] > int(self.level)):
if verify:
self.debugfile.write("m.debug "+self.format2Str(args)+" >? "+str(self.level)+"\n")
if (args["mlevel"] - args["mprio"] > int(self.level)):
return
if (args["mprio"] + 20) % 2 == 1:
print(args["mtext"])

41
test/test_11message.py

@ -12,7 +12,7 @@ import basic.message
HOME_PATH = test.constants.HOME_PATH
PYTHON_CMD = "python"
TEST_FUNCTIONS = ["test_00init", "test_04logLevel", "test_05loginigArgs", "test_10set", "test_11log", "test_20close"]
#TEST_FUNCTIONS = ["test_00init", "test_04logLevel"]
TEST_FUNCTIONS = ["test_00init", "test_05loginigArgs"]
PROGRAM_NAME = "clean_workspace"
NOT_TO_LOG = ["xx1xx", "xx4xx"]
@ -87,6 +87,7 @@ class MyTestCase(unittest.TestCase):
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
# setattr(msgObject.job.par, "tool", "msg_tool")
res = msgObject.getLoggingArgs(12, 1, "text")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 1)
@ -98,8 +99,38 @@ class MyTestCase(unittest.TestCase):
res = msgObject.getLoggingArgs(12, "text", "ERROR: ")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual(res["mtext"], "ERROR: text")
cnttest += 9
self.assertEqual("ERROR: text", res["mtext"])
args = ["a", "b", "c"]
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("[a, b, c]", res["mtext"])
res = msgObject.getLoggingArgs(12, 1, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 1)
self.assertEqual("[a, b, c]", res["mtext"])
args = {"a": "x", "b": "y", "c": "z"}
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: y, c: z}", res["mtext"])
res = msgObject.getLoggingArgs(12, 3, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 3)
self.assertEqual("{a: x, b: y, c: z}", res["mtext"])
args = {"a": "x", "b": {"b1": "x", "b2": "y"}, "c": ["x", "y", "z"] }
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: {b1: x, b2: y}, c: [x, y, z]}", res["mtext"])
res = msgObject.getLoggingArgs(12, 0, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: {b1: x, b2: y}, c: [x, y, z]}", res["mtext"])
cnttest += 18
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_10set(self):
@ -190,8 +221,8 @@ class MyTestCase(unittest.TestCase):
self.assertEqual(True, msgObject.logfile.closed)
self.assertEqual(True, msgObject.debugfile.closed)
# now check what is logged
logtext = tools.file_tool.readFileText(jobObject, msgObject.logpath, None)
debugtext = tools.file_tool.readFileText(jobObject, msgObject.debugpath, None)
logtext = tools.file_tool.read_file_text(jobObject, msgObject.logpath, None)
debugtext = tools.file_tool.read_file_text(jobObject, msgObject.debugpath, None)
for x in TO_LOG:
regex = r".*" + x + ""
self.assertIn(x, logtext)

0
tools/job_const.py

Loading…
Cancel
Save