pgai / call_logger.py
Beracles's picture
logger hostname
4f51444
import json
import requests
from pgsoft.pgdate.date_utils import beijing
url_base = "https://pgsoft-logger.hf.space"
url_add = f"{url_base}/add"
def post_to_logger_helper(log, caller, token):
header = {
"accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
myobj = {
"log": log,
"caller": caller,
}
try:
res = requests.post(url_add, headers=header, json=myobj)
content = res.json()
if res.status_code == 200:
timestamp = content["timestamp"]
command = content["command"]
status = content["status"]
print(f"[{timestamp}] [{command}] [{status}]")
return True
else:
print(res.text)
return False
except Exception as e:
print(f"{type(e)}: {e}")
return False
def call_logger(log_info, caller, token) -> None:
#######################
# logging
#######################
calling_start = beijing()
print(f"[{calling_start}] logging starts")
#################################################
res = post_to_logger_helper(json.dumps(log_info), caller, token)
if res:
print(f"[logging to {url_base}] OK")
else:
print(f"[logging to {url_base}] Failed")
#################################################
calling_end = beijing()
timecost = calling_end.timestamp() - calling_start.timestamp()
print(f"[{calling_end}] logging ends, costs {timecost:.2f}s")