From 64a4473f399192c615d08ff06ebf8326c4c6c4b1 Mon Sep 17 00:00:00 2001 From: aguilard Date: Tue, 18 Apr 2023 16:29:22 +0000 Subject: [PATCH] Feature 10981: use Python library for logging in Webhook Change-Id: Ib60ef2005618f94da34da1910fb26f95d77bb7a2 Signed-off-by: aguilard --- .../src/osm_webhook_translator/main.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/osm_webhook_translator/src/osm_webhook_translator/main.py b/osm_webhook_translator/src/osm_webhook_translator/main.py index 587fa9c..6e9e718 100644 --- a/osm_webhook_translator/src/osm_webhook_translator/main.py +++ b/osm_webhook_translator/src/osm_webhook_translator/main.py @@ -15,6 +15,7 @@ # limitations under the License. ####################################################################################### from datetime import datetime +import logging import os from random import randint @@ -22,6 +23,12 @@ from fastapi import FastAPI import requests +logging.basicConfig( + format="%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(message)s", + datefmt="%Y/%m/%d %H:%M:%S", +) +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) app = FastAPI() @@ -37,16 +44,16 @@ def send_to_airflow(output_endpoint, content): rnd = str(randint(0, 999999)).rjust(6, "0") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") dag_run_id = output_endpoint + "_" + timestamp + "_" + rnd - print(f"HTTP POST {url}...") + logger.info(f"HTTP POST {url}") req = requests.post( url=url, auth=(airflow_user, airflow_pass), json={"dag_run_id": dag_run_id, "conf": content}, ) - print(req.text) + logger.info(f"Response: {req.text}") # timeout and retries except Exception as e: - print(f"HTTP error: {repr(e)}") + logger.error(f"HTTP error: {repr(e)}") raise requests.HTTPException(status_code=403, detail=repr(e)) -- 2.25.1