Skip to content

Commit 8cc53e4

Browse files
committed
remove unnecessary logging
1 parent 259ebac commit 8cc53e4

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

portality/lib/plausible.py

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,46 @@ def create_logfile(log_dir=None):
3030
def send_event(goal: str, on_completed=None, **props_kwargs):
3131
""" Send event data to Plausible Analytics. (ref: https://plausible.io/docs/events-api )
3232
"""
33-
if app.config.get("DOAJENV") == 'dev':
34-
print(f"Plausible Analytics Event: Goal: {goal}, Payload: {props_kwargs}")
35-
else:
36-
plausible_api_url = app.config.get('PLAUSIBLE_API_URL', '')
37-
if not app.config.get('PLAUSIBLE_URL', '') and not plausible_api_url:
38-
global _failstate
39-
if not _failstate:
40-
logger.warning('skip send_event, PLAUSIBLE_URL undefined')
41-
_failstate = True
42-
return
43-
44-
# prepare request payload
45-
payload = {'name': goal,
46-
'url': app.config.get('BASE_URL', 'http://localhost'),
47-
'domain': app.config.get('PLAUSIBLE_SITE_NAME', 'localhost'), }
48-
if props_kwargs:
49-
payload['props'] = props_kwargs
50-
51-
# headers for plausible API
52-
headers = {'Content-Type': 'application/json'}
53-
if request:
54-
# Add IP from CloudFlare header or remote_addr - this works because we have ProxyFix on the app
55-
headers["X-Forwarded-For"] = request.headers.get("cf-connecting-ip", request.remote_addr)
56-
user_agent_key = 'User-Agent'
57-
user_agent_val = request.headers.get(user_agent_key)
58-
if user_agent_val:
59-
headers[user_agent_key] = user_agent_val
60-
61-
# Supply detailed URL if we have it from the request context
62-
payload['url'] = request.base_url
63-
64-
def _send():
65-
resp = requests.post(plausible_api_url, json=payload, headers=headers)
66-
if resp.status_code >= 300:
67-
logger.warning(f'Send plausible event API fail. snd: [{resp.url}] [{headers}] [{payload}] rcv: [{resp.status_code}] [{resp.text}]')
68-
if on_completed:
69-
on_completed(resp)
70-
71-
try:
72-
Thread(target=_send).start()
73-
except RuntimeError as e:
74-
# When we can't create a thread; don't escalate further since we'd rather the app works than the analytics
75-
logger.error(str(e))
33+
plausible_api_url = app.config.get('PLAUSIBLE_API_URL', '')
34+
if not app.config.get('PLAUSIBLE_URL', '') and not plausible_api_url:
35+
global _failstate
36+
if not _failstate:
37+
logger.warning('skip send_event, PLAUSIBLE_URL undefined')
38+
_failstate = True
39+
return
40+
41+
# prepare request payload
42+
payload = {'name': goal,
43+
'url': app.config.get('BASE_URL', 'http://localhost'),
44+
'domain': app.config.get('PLAUSIBLE_SITE_NAME', 'localhost'), }
45+
if props_kwargs:
46+
payload['props'] = props_kwargs
47+
48+
# headers for plausible API
49+
headers = {'Content-Type': 'application/json'}
50+
if request:
51+
# Add IP from CloudFlare header or remote_addr - this works because we have ProxyFix on the app
52+
headers["X-Forwarded-For"] = request.headers.get("cf-connecting-ip", request.remote_addr)
53+
user_agent_key = 'User-Agent'
54+
user_agent_val = request.headers.get(user_agent_key)
55+
if user_agent_val:
56+
headers[user_agent_key] = user_agent_val
57+
58+
# Supply detailed URL if we have it from the request context
59+
payload['url'] = request.base_url
60+
61+
def _send():
62+
resp = requests.post(plausible_api_url, json=payload, headers=headers)
63+
if resp.status_code >= 300:
64+
logger.warning(f'Send plausible event API fail. snd: [{resp.url}] [{headers}] [{payload}] rcv: [{resp.status_code}] [{resp.text}]')
65+
if on_completed:
66+
on_completed(resp)
67+
68+
try:
69+
Thread(target=_send).start()
70+
except RuntimeError as e:
71+
# When we can't create a thread; don't escalate further since we'd rather the app works than the analytics
72+
logger.error(str(e))
7673

7774

7875
def pa_event(goal, action, label='',

0 commit comments

Comments
 (0)