我正在使用Flask(python 2),并且添加了如下的mylog:
def my_enqueue(self, record, message, resource=None, labels=None, trace=None, span_id=None):
queue_entry = {
"info": {"message": message, "python_logger": record.name},
"severity": _helpers._normalize_severity(record.levelno),
"resource": resource,
"labels": labels,
"trace": trace,
"span_id": span_id,
"timestamp": datetime.datetime.utcfromtimestamp(record.created),
}
# if 'custom_fields' in record:
# if getattr(record, "extra", None):
if session:
user_id = "Anonymous"
if "user_id" in session:
user_id = session['user_id']
extra = {
"url": request.path,
"method": request.method,
"ip": request.environ.get("REMOTE_ADDR"),
"user_id": user_id,
}
queue_entry['info']['extra'] = extra
print(11)
self._queue.put_nowait(queue_entry)
_Worker.enqueue = my_enqueue
client = google.cloud.logging.Client()
def mylog(msg, level="info", name=None):
try:
if not name:
name = inspect.stack()[1][3]
handler = CloudLoggingHandler(client, name=name)
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.INFO)
cloud_logger.handlers = []
cloud_logger.addHandler(handler)
if level == "info":
cloud_logger.info(msg) # API call
if level == "error":
cloud_logger.error(msg) # API call
except Exception, e:
print("Error mylog")
print(e)
我在一个全天连续运行的脚本中使用mylog。每天,最终,我不得不终止脚本,然后减少RAM使用量。否则,由于RAM使用量增加,系统将崩溃。我尝试运行相同的脚本而不进行日志记录,并且没有增加RAM使用率。
能告诉我我在这里犯了什么错误,这样它就不会吃光所有RAM。