problem description
using the logging module of python, the log of requests will be printed. Now the log level of reqyests has been set to warning, takes effect in macos, but the information requested by the network will still be printed on the server centos.
the environmental background of the problems and what methods you have tried
related codes
/ / Please paste the code text below (do not replace the code with pictures)
import logging
import datetime
import os
from threading import Thread
from logic.manager import LOG_PATH
import multiprocessing
def singleton(cls):
instances = {}
def getinstance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return getinstance
@singleton
class LogHandler(object):
def __init__(self, task_log_id, pid, file_name):
self.log_queue = multiprocessing.Queue()
self.logger = logging.getLogger()
self.msg = ""
self.msg_list = list()
self.log_id = ""
self.set_formatter(task_log_id, pid, file_name)
self.run()
def set_formatter(self, task_log_id, pid, file_name):
"""
:param task_log_id:
:param pid:
:param file_name:
:return:
"""
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("tornado").setLevel(logging.WARNING)
logging.getLogger("urllib").setLevel(logging.WARNING)
logging.getLogger("urllib2").setLevel(logging.WARNING)
today = str(datetime.date.today())
daily_log_path = os.path.join(LOG_PATH, today)
if not os.path.exists(daily_log_path):
os.makedirs(daily_log_path)
handler = logging.FileHandler(os.path.join(daily_log_path, file_name))
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"""LEVEL:%(levelname)s DATE:%(asctime)s PID:{0} taskLogId:{1} %(message)s""".format(pid, task_log_id))
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.DEBUG)
logging.StreamHandler().setLevel(logging.WARNING)
def push(self, msg):
"""
push log layout message to self.msg_list
:param msg:
:return:
"""
self.msg_list.append(msg)
self.msg += msg + " "
def pop(self):
"""
remove last log layout message from self.msg_list
:return:
"""
self.msg_list.pop()
self.msg = ""
for msg in self.msg_list:
self.msg += msg + " "
def debug(self, msg):
self.joint_info(level="debug", msg=msg)
def warning(self, msg):
self.joint_info(level="warning", msg=msg)
def info(self, msg):
self.joint_info(level="info", msg=msg)
def error(self, msg):
self.joint_info(level="error", msg=msg)
def joint_info(self, level, msg):
info = {
"level": level,
"msg": self.msg + msg
}
self.log_queue.put(info)
def export_log(self):
"""
export log to file
:return:
"""
while True:
log_info = self.log_queue.get()
level = log_info["level"]
msg = log_info["msg"]
if "jobDone" in msg:
break
getattr(self.logger, level)(msg)
-sharp cs_log.send(log_id=self.log_id, )
def run(self):
td = Thread(target=self.export_log)
td.start()
what result do you expect? What is the error message actually seen?
LEVEL:DEBUG DATE:2018-08-14 14 14 5f89 29 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e STREAM baggage IHDR" 16 13
LEVEL:DEBUG DATE:2018-08-14 14 14 29 PID:28841 taskLogId:28478b57 53755 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e STREAM bounded IDAT" 41 8192
LEVEL:DEBUG DATE:2018-08-14 142929 LEVEL:DEBUG DATE:2018 53756 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e GET http://127.0.0.1:34726/session/49c9b09187892e9fa9ea19a2b7b4c727/element/0.18678801050348826-2/size {"sessionId": "49c9b09187892e9fa9ea19a2b7b4c727", "id": "0.18678801050348826-2"}
LEVEL:DEBUG DATE:2018-08-14 14 14 id 29 br 53766 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e Finished Request
LEVEL:DEBUG DATE:2018-08-14 14 14 id 29 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e GET http://127.0.0.1:34726/session/49c9b09187892e9fa9ea19a2b7b4c727/element/0.18678801050348826- 2/size {"sessionId": "49c9b09187892e9fa9ea19a2b7b4c727", "id": "0.18678801050348826-2"}
LEVEL:DEBUG DATE:2018-08-14 14 14 LEVEL:DEBUG DATE:2018 29 PID:28841 taskLogId:28478b57-5f89-4406-b161-ec352ec01e8e Finished Request