Spaces:
Sleeping
Sleeping
fix logging
Browse files- backend/logging_utils.py +29 -6
backend/logging_utils.py
CHANGED
|
@@ -28,13 +28,14 @@ try:
|
|
| 28 |
except Exception as e:
|
| 29 |
logging.warning(f"Could not set up file logging: {e}")
|
| 30 |
|
| 31 |
-
def log_category_usage(category: Optional[str] = None):
|
| 32 |
-
"""Log the usage of a category."""
|
| 33 |
if not os.access(log_dir, os.W_OK):
|
| 34 |
logging.warning("Log directory is not writable, skipping category usage logging")
|
| 35 |
return
|
| 36 |
|
| 37 |
stats_file = log_dir / "category_stats.json"
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
if stats_file.exists():
|
|
@@ -43,12 +44,34 @@ def log_category_usage(category: Optional[str] = None):
|
|
| 43 |
else:
|
| 44 |
stats = {}
|
| 45 |
|
| 46 |
-
# Initialize
|
| 47 |
category = category or "default"
|
| 48 |
-
if category in stats:
|
| 49 |
-
stats[category]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
-
stats[category]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Save updated stats
|
| 54 |
with open(stats_file, 'w') as f:
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
logging.warning(f"Could not set up file logging: {e}")
|
| 30 |
|
| 31 |
+
def log_category_usage(category: Optional[str] = None, endpoint: Optional[str] = None, success: bool = True):
|
| 32 |
+
"""Log the usage of a category with endpoint and success information."""
|
| 33 |
if not os.access(log_dir, os.W_OK):
|
| 34 |
logging.warning("Log directory is not writable, skipping category usage logging")
|
| 35 |
return
|
| 36 |
|
| 37 |
stats_file = log_dir / "category_stats.json"
|
| 38 |
+
timestamp = datetime.now().isoformat()
|
| 39 |
|
| 40 |
try:
|
| 41 |
if stats_file.exists():
|
|
|
|
| 44 |
else:
|
| 45 |
stats = {}
|
| 46 |
|
| 47 |
+
# Initialize category if not exists
|
| 48 |
category = category or "default"
|
| 49 |
+
if category not in stats:
|
| 50 |
+
stats[category] = {
|
| 51 |
+
"total_requests": 0,
|
| 52 |
+
"successful_requests": 0,
|
| 53 |
+
"failed_requests": 0,
|
| 54 |
+
"endpoints": {},
|
| 55 |
+
"last_used": None
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Update category stats
|
| 59 |
+
stats[category]["total_requests"] += 1
|
| 60 |
+
if success:
|
| 61 |
+
stats[category]["successful_requests"] += 1
|
| 62 |
else:
|
| 63 |
+
stats[category]["failed_requests"] += 1
|
| 64 |
+
|
| 65 |
+
# Update endpoint stats
|
| 66 |
+
if endpoint:
|
| 67 |
+
if "endpoints" not in stats[category]:
|
| 68 |
+
stats[category]["endpoints"] = {}
|
| 69 |
+
if endpoint not in stats[category]["endpoints"]:
|
| 70 |
+
stats[category]["endpoints"][endpoint] = 0
|
| 71 |
+
stats[category]["endpoints"][endpoint] += 1
|
| 72 |
+
|
| 73 |
+
# Update timestamp
|
| 74 |
+
stats[category]["last_used"] = timestamp
|
| 75 |
|
| 76 |
# Save updated stats
|
| 77 |
with open(stats_file, 'w') as f:
|