Skip to content

Commit

Permalink
log attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
wslyvh committed Aug 21, 2024
1 parent 7060025 commit 89db10d
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions pretalx_webhook/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

logger = logging.getLogger(__name__)

class CustomJSONEncoder(DjangoJSONEncoder):
def default(self, obj):
if hasattr(obj, '__dict__'):
return {key: self.default(value) for key, value in vars(obj).items()
if not key.startswith('_') and not callable(value)}
elif hasattr(obj, 'isoformat'):
return obj.isoformat()
return super().default(obj)

def log_object_attributes(obj, logger):
try:
attributes = json.dumps(obj, cls=CustomJSONEncoder, indent=2)
logger.error(f"Object attributes for {type(obj).__name__}:\n{attributes}")
except Exception as e:
logger.error(f"Error logging attributes for {type(obj).__name__}: {str(e)}")


@receiver(schedule_release, dispatch_uid="pretalx_webhook_schedule_release")
def on_schedule_release(sender, schedule, user, **kwargs):
try:
Expand All @@ -25,36 +42,35 @@ def on_schedule_release(sender, schedule, user, **kwargs):
logger.info(f"Webhook endpoint is empty for event {sender.slug}")
return

logger.error(f"Signal arguments..")
logger.error(str(sender))
logger.error(str(schedule))
logger.error(str(user))
logger.error(str(kwargs))

# log the arguments
logger.info(f"Prepare the payload..")
logger.info(f"Log all arguments..")
log_object_attributes(sender, logger)
log_object_attributes(schedule, logger)
log_object_attributes(user, logger)
log_object_attributes(kwargs, logger)

payload = {
'sender': sender,
'schedule': schedule,
'user': user,
'sender': str(sender),
'schedule': str(schedule),
'user': str(user),
}

logger.error(f"Prepare payload..")
logger.error(payload)
logger.error(str(payload))
logger.error(json.dumps(payload))

headers = {'Content-Type': 'application/json'}
if webhook_secret:
headers['X-Webhook-Secret'] = webhook_secret
else:
logger.warning(f"Webhook secret is empty for event {sender.slug}")

# Send the POST request to the webhook endpoint

logger.error(f"Send JSON request..")
response = requests.post(webhook_endpoint,
json=json.dumps(payload),
headers=headers,
)

# Log the response
if response.status_code == 200:
logger.info(f"Webhook sent successfully for event {sender.slug}")
else:
Expand Down

0 comments on commit 89db10d

Please sign in to comment.