ftest-common-py/ftest_common/logger.py

54 lines
1.4 KiB
Python

# SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from urllib.parse import urlparse, urlunparse
import logging
import requests
LOG_FILE = "ap.log"
def configure_logger():
logger = logging.getLogger("ap")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(LOG_FILE)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
return logger
logger = configure_logger()
def upload_logs_to_ftest(success: bool, logs: str):
from .env import FTEST_HOST, FTEST_AUTH
parsed_ftest_host = urlparse(FTEST_HOST)
ftest = urlunparse(
(
parsed_ftest_host.scheme,
parsed_ftest_host.netloc,
f"/api/v1/{FTEST_AUTH}/results",
"",
"",
"",
)
)
logger.info(f"Uploading logs to ftest server {ftest}")
payload = {"success": success, "logs": logs}
res = requests.post(ftest, json=payload, headers={"Origin": "http://example.org"})
if res.status_code == 200:
logger.info("Upload successful")
else:
print(res)