Skip to content

Instantly share code, notes, and snippets.

@soodoku
Last active July 16, 2023 17:36
Show Gist options
  • Save soodoku/34c083cd0cd5a3162f4fea8caefb2a54 to your computer and use it in GitHub Desktop.
Save soodoku/34c083cd0cd5a3162f4fea8caefb2a54 to your computer and use it in GitHub Desktop.
Remote Logging of Errors With User Approval

Log Errors Remotely With User Approval

"The modern software revolution hasn’t caught up with the open-source research software community. Most open-source research software is still distributed as a binary and emits no logs that can be analyzed by the developer."

From: https://gojiberries.io/2023/07/02/hard-problems-about-research-software/

We provide sample code here for how remote logging with user approval can be enabled.

from flask import Flask, request
import uuid
app = Flask(__name__)
@app.route('/error_endpoint', methods=['POST'])
def receive_error_message():
error_message = request.form.get('error_message')
if error_message:
unique_filename = str(uuid.uuid4()) + ".txt"
with open(unique_filename, 'w') as file:
file.write(error_message)
return "Error message received and stored successfully!"
else:
return "Error: No error message received."
if __name__ == '__main__':
app.run(port=5001)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "bca81252-8412-4d47-b486-163ff9d3a469",
"metadata": {},
"source": [
"### Log Errors Remotely"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d300329d-4386-46a3-85fa-9f4506f9fa4c",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"def send_error_message(error_message):\n",
" try:\n",
" 1/0\n",
"\n",
" except Exception as e:\n",
" print(\"An error occurred:\", e)\n",
" choice = input(\"Do you want to send the error message to the server? (y/n): \")\n",
"\n",
" if choice.lower() == 'y':\n",
" try:\n",
" url = \"https://soodoku.pythonanywhere.com/error_endpoint\"\n",
" response = requests.post(url, data={'error_message': error_message})\n",
" if response.status_code == 200:\n",
" print(\"Error message sent successfully!\")\n",
" else:\n",
" print(\"Failed to send the error message. Please try again later.\")\n",
" except requests.exceptions.RequestException:\n",
" print(\"An error occurred while sending the request.\")\n",
" else:\n",
" print(\"Error message not sent.\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ca454fbc-2cfc-47ed-87c1-d623ca720e2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"An error occurred: division by zero\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to send the error message to the server? (y/n): y\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error message sent successfully!\n"
]
}
],
"source": [
"send_error_message(error_message = \"hello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e96fc346-1d1a-4d71-bf99-2db54ef3deb8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment