Spaces:
Running
Running
File size: 751 Bytes
74626f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import os
from datetime import datetime
from flask import request
LOG_FILE = os.path.join(os.path.dirname(__file__), 'backend_logs.txt')
def log_request(info=None):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
method = request.method
url = request.url
log_entry = f"[{timestamp}] {method} {url}\n"
if info:
log_entry += f"Info: {info}\n"
if method in ['POST', 'PUT']:
try:
json_data = request.get_json(silent=True)
if json_data:
log_entry += f"Payload: {json_data}\n"
except Exception:
pass
log_entry += "-" * 50 + "\n"
with open(LOG_FILE, 'a', encoding='utf-8') as f:
f.write(log_entry)
|