File size: 1,556 Bytes
b4cd98c
5d5cb75
b4cd98c
 
 
4f51444
5d5cb75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4cd98c
 
 
 
 
 
5d5cb75
 
 
 
 
b4cd98c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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")