1
0
Fork 0
mirror of https://github.com/ThomasGsp/HyperProxmox.git synced 2025-02-14 20:22:13 +00:00

Log system core

This commit is contained in:
Tlams 2018-03-22 15:33:06 +00:00
parent f09356af93
commit 724b5c0f72

View file

@ -4,7 +4,7 @@ import sys
import http.client as http_client
import datetime
import re
import json
class StreamToLogger(object):
def __init__(self, logger, log_level=logging.INFO):
@ -83,14 +83,47 @@ class Logger(object):
errorlog.close()
"""
def vtype(verbose):
switcher = {
1: "INFO",
2: "WARNING",
3: "ERROR",
4: "CRITICAL",
5: "DEBUG"
}
return switcher.get(verbose, "DEBUG")
"""
class Logger2:
def __init__(self, loggerconf):
self.logs_dir = loggerconf['logs_dir']
self.log_level = int(loggerconf['logs_level'])
def write(self, json_text):
switcher = {
1: "INFO",
2: "WARNING",
3: "ERROR",
4: "CRITICAL",
5: "DEBUG"
}
lKeyC = [key for key, value in switcher.items() if '"{val}"'.format(val=value) == json.dumps(json_text["result"])][0]
if lKeyC >= self.log_level and lKeyC <= 4:
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d %H:%M")
text = re.sub(r"ticket\?(.*?) ", "ticket?username=***YOUR_USER***&password=***PWD***", json_text["value"])
try:
info = "[{0}] [{1}] [{2}]".format(json_text["result"], json_text["type"], json_text["target"])
except BaseException:
info = "[{0}] [{1}]".format(json_text["result"], json_text["type"])
text = "[{date}] {info} : {text} \n".format(date=date, info=info, text=text)
if json_text["type"] == "PROXMOX":
errorlog = open("{0}/proxmox.log".format(self.logs_dir), "ab")
errorlog.write(text.encode('utf-8'))
elif json_text["type"] == "HYPERPROXMOX":
errorlog = open("{0}/hyperproxmox.log".format(self.logs_dir), "ab")
errorlog.write(text.encode('utf-8'))
elif json_text["type"] == "PYTHON":
errorlog = open("{0}/python.log".format(self.logs_dir), "ab")
errorlog.write(text.encode('utf-8'))
else:
errorlog = open("{0}/others.log".format(self.logs_dir), "ab")
errorlog.write(text.encode('utf-8'))
errorlog.close()