Skip to content

Commit 6f14975

Browse files
committed
Fix #31 log to stdout
1 parent c767c87 commit 6f14975

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

main.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
load_dotenv(verbose=True)
1010

1111
PYTHON_LOG_LEVEL = os.environ.get("PYTHON_LOG_LEVEL", "WARNING")
12+
log = logging.getLogger(__name__)
13+
log.setLevel(PYTHON_LOG_LEVEL)
14+
log.addHandler(logging.StreamHandler())
1215

1316
REDIS_HOSTNAME = os.environ.get("REDIS_HOSTNAME")
1417
REDIS_PORT = os.environ.get("REDIS_PORT")
@@ -19,8 +22,6 @@
1922

2023
print(f"PYTHON_LOG_LEVEL is: {PYTHON_LOG_LEVEL}")
2124

22-
logging.basicConfig(level=PYTHON_LOG_LEVEL)
23-
2425
app = Flask(__name__)
2526

2627

@@ -33,7 +34,6 @@ def route_stripe_connect_webhook():
3334
- Returns status code & response body from shop
3435
- (Only returns 200 if the shop returns 200, otherwise return 400)
3536
"""
36-
3737
load_dotenv(verbose=True)
3838
redisConn = redis.Redis(
3939
host=REDIS_HOSTNAME,
@@ -42,20 +42,21 @@ def route_stripe_connect_webhook():
4242
socket_timeout=REDIS_TIMEOUT_SECS, # noqa E501
4343
)
4444
try:
45+
log.debug("Attempting to parse account from request")
4546
stripe_connect_account_id = request.json["account"]
4647
except KeyError as e:
47-
logging.error(e)
48+
log.error(e)
4849
msg = f"Not a connect request.\
4950
No 'account' property in payload\n\n{request.json}"
50-
logging.error(msg)
51+
log.error(msg)
5152
return msg, 422
5253
try:
5354
stripe_connect_account_id = request.json["account"]
5455

5556
# Get shop url from redis via stripe connect account id
5657
site_url = redisConn.get(stripe_connect_account_id)
57-
logging.debug(f"Routing for account: {stripe_connect_account_id}")
58-
logging.debug(f"Will be routing webhook to: {site_url}")
58+
log.debug(f"Routing for account: {stripe_connect_account_id}")
59+
log.debug(f"Will be routing webhook to: {site_url}")
5960

6061
if site_url is not None:
6162
# Verify stripe signature header
@@ -69,44 +70,43 @@ def route_stripe_connect_webhook():
6970
request.data, sig_header, STRIPE_WEBHOOK_SECRET
7071
)
7172
except ValueError as e:
72-
logging.error(
73-
"ValueError when attempting to get Stripe-Signature header"
74-
)
75-
logging.error(e)
73+
msg = "ValueError when attempting to get Stripe-Signature header" # noqa: E501
74+
log.error(msg)
75+
log.error(e)
7676
return e, 400
7777
except stripe.error.SignatureVerificationError as e:
78-
logging.error("Stripe SignatureVerificationError")
79-
logging.error(e)
78+
log.error("Stripe SignatureVerificationError")
79+
log.error(e)
8080
return "Stripe SignatureVerificationError", 400
8181

8282
post_url = site_url.decode("utf-8") + STRIPE_WEBHOOK_PATH
83-
logging.debug(f"Posting webhook to: {post_url}")
83+
log.debug(f"Posting webhook to: {post_url}")
8484

8585
resp = requests.post(
8686
post_url,
8787
json=event,
8888
)
8989
# Return (proxying) whatever the site (shop) responds
9090
# including the return code
91-
logging.debug(f"{resp.status_code}, {resp.text}")
91+
log.debug(f"{resp.status_code}, {resp.text}")
9292
return resp.text, resp.status_code
9393
else:
9494
msg = f'{{"msg": "No site_url for that account id", \
9595
"account id": "{stripe_connect_account_id}"}}'
9696

97-
logging.error(msg)
97+
log.error(msg)
9898
return Response(msg, status=500, mimetype="application/json")
9999

100100
except redis.exceptions.ResponseError as e:
101-
logging.error("Redis ResponseError")
102-
logging.error(e)
101+
log.error("Redis ResponseError")
102+
log.error(e)
103103
except redis.exceptions.ConnectionError as e:
104-
logging.error("Redis ConnectionError")
105-
logging.error(e)
104+
log.error("Redis ConnectionError")
105+
log.error(e)
106106
except redis.exceptions.TimeoutError as e:
107-
logging.error("Redis timeout")
108-
logging.error(e)
107+
log.error("Redis timeout")
108+
log.error(e)
109109
except Exception as e:
110-
logging.error("Error processing stripe webhook request")
111-
logging.error(e)
110+
log.error("Error processing stripe webhook request")
111+
log.error(e)
112112
return "Invalid request to route_stripe_connect_webhook", 400

0 commit comments

Comments
 (0)