Spaces:
Sleeping
Sleeping
fix logging
Browse files- backend/logging_utils.py +52 -69
backend/logging_utils.py
CHANGED
|
@@ -6,87 +6,70 @@ import os
|
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
-
|
| 10 |
-
log_dir.
|
|
|
|
| 11 |
|
| 12 |
# Configure file handler for general logs
|
| 13 |
logging.basicConfig(
|
| 14 |
level=logging.INFO,
|
| 15 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 16 |
handlers=[
|
| 17 |
-
logging.
|
| 18 |
-
logging.StreamHandler()
|
| 19 |
]
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
if
|
| 33 |
-
with open(
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
logging.error(f"Error
|
| 37 |
-
return {}
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
-
logging.error(f"Error
|
| 46 |
-
|
| 47 |
-
def log_category_usage(category: Optional[str], endpoint: str, success: bool):
|
| 48 |
-
"""Log category usage with detailed metrics."""
|
| 49 |
-
timestamp = datetime.now().isoformat()
|
| 50 |
-
|
| 51 |
-
# Load existing stats
|
| 52 |
-
stats = load_category_stats()
|
| 53 |
-
|
| 54 |
-
# Initialize category if not exists
|
| 55 |
-
category_key = category or "default"
|
| 56 |
-
if category_key not in stats:
|
| 57 |
-
stats[category_key] = {
|
| 58 |
-
"total_requests": 0,
|
| 59 |
-
"successful_requests": 0,
|
| 60 |
-
"failed_requests": 0,
|
| 61 |
-
"endpoints": {},
|
| 62 |
-
"last_used": None
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
# Update category stats
|
| 66 |
-
stats[category_key]["total_requests"] += 1
|
| 67 |
-
if success:
|
| 68 |
-
stats[category_key]["successful_requests"] += 1
|
| 69 |
-
else:
|
| 70 |
-
stats[category_key]["failed_requests"] += 1
|
| 71 |
-
|
| 72 |
-
# Update endpoint stats
|
| 73 |
-
if endpoint not in stats[category_key]["endpoints"]:
|
| 74 |
-
stats[category_key]["endpoints"][endpoint] = 0
|
| 75 |
-
stats[category_key]["endpoints"][endpoint] += 1
|
| 76 |
-
|
| 77 |
-
# Update timestamp
|
| 78 |
-
stats[category_key]["last_used"] = timestamp
|
| 79 |
-
|
| 80 |
-
# Save updated stats
|
| 81 |
-
save_category_stats(stats)
|
| 82 |
-
|
| 83 |
-
# Log the event
|
| 84 |
-
category_logger.info(
|
| 85 |
-
f"Category usage - Category: {category_key}, "
|
| 86 |
-
f"Endpoint: {endpoint}, "
|
| 87 |
-
f"Success: {success}"
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
def get_category_statistics():
|
| 91 |
-
"""Get category usage statistics."""
|
| 92 |
-
return load_category_stats()
|
|
|
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
+
# Use /tmp for logs in production (e.g. Hugging Face Spaces) or local logs dir in development
|
| 10 |
+
log_dir = Path("/tmp/schematic_ai_logs") if os.environ.get("SPACE_ID") else Path(__file__).parent.parent / "logs"
|
| 11 |
+
log_dir.mkdir(exist_ok=True, parents=True)
|
| 12 |
|
| 13 |
# Configure file handler for general logs
|
| 14 |
logging.basicConfig(
|
| 15 |
level=logging.INFO,
|
| 16 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 17 |
handlers=[
|
| 18 |
+
logging.StreamHandler() # Always log to console
|
|
|
|
| 19 |
]
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Only add file handler if we can write to the directory
|
| 23 |
+
try:
|
| 24 |
+
if os.access(log_dir, os.W_OK):
|
| 25 |
+
file_handler = logging.FileHandler(log_dir / "app.log")
|
| 26 |
+
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 27 |
+
logging.getLogger().addHandler(file_handler)
|
| 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():
|
| 41 |
+
with open(stats_file, 'r') as f:
|
| 42 |
+
stats = json.load(f)
|
| 43 |
+
else:
|
| 44 |
+
stats = {}
|
| 45 |
+
|
| 46 |
+
# Initialize or increment category count
|
| 47 |
+
category = category or "default"
|
| 48 |
+
if category in stats:
|
| 49 |
+
stats[category] += 1
|
| 50 |
+
else:
|
| 51 |
+
stats[category] = 1
|
| 52 |
+
|
| 53 |
+
# Save updated stats
|
| 54 |
+
with open(stats_file, 'w') as f:
|
| 55 |
+
json.dump(stats, f, indent=4)
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
+
logging.error(f"Error logging category usage: {e}")
|
|
|
|
| 59 |
|
| 60 |
+
def get_category_statistics():
|
| 61 |
+
"""Get the usage statistics for all categories."""
|
| 62 |
+
if not os.access(log_dir, os.W_OK):
|
| 63 |
+
logging.warning("Log directory is not writable, cannot read category statistics")
|
| 64 |
+
return {}
|
| 65 |
+
|
| 66 |
+
stats_file = log_dir / "category_stats.json"
|
| 67 |
+
|
| 68 |
try:
|
| 69 |
+
if stats_file.exists():
|
| 70 |
+
with open(stats_file, 'r') as f:
|
| 71 |
+
return json.load(f)
|
| 72 |
+
return {}
|
| 73 |
except Exception as e:
|
| 74 |
+
logging.error(f"Error reading category statistics: {e}")
|
| 75 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|