Spaces:
Runtime error
Runtime error
Download utils/logging/logger_setup.py from Darshankumarr03/Swift-Query: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/Darshankumarr03/Swift-Query/resolve/main/utils/logging/logger_setup.py
- Command line
-
hf download hf://spaces/Darshankumarr03/Swift-Query/utils/logging/logger_setup.py
-
curl -L -o logger_setup.py https://huggingface.co/spaces/Darshankumarr03/Swift-Query/resolve/main/utils/logging/logger_setup.py
1.48 kB
| import logging | |
| import json | |
| import os | |
| from datetime import datetime | |
| # --- JSON Logging Setup --- | |
| class JsonFormatter(logging.Formatter): | |
| def format(self, record): | |
| log_record = { | |
| "timestamp": datetime.fromtimestamp(record.created).isoformat(), | |
| "level": record.levelname, | |
| "message": record.getMessage() | |
| } | |
| # If the message is a dict, merge it into the log record | |
| if isinstance(record.msg, dict): | |
| log_record.update(record.msg) | |
| # Remove the original message field to avoid redundancy if it's just the dict string representation | |
| if "message" in log_record and log_record["message"] == str(record.msg): | |
| del log_record["message"] | |
| return json.dumps(log_record) | |
| def setup_logger(): | |
| # Create log directory | |
| log_dir = os.path.join('logs', 'user_interaction') | |
| os.makedirs(log_dir, exist_ok=True) | |
| log_file_path = os.path.join(log_dir, 'events.jsonl') | |
| log_handler = logging.FileHandler(log_file_path) | |
| log_handler.setFormatter(JsonFormatter()) | |
| logger = logging.getLogger('action_logger') | |
| logger.addHandler(log_handler) | |
| logger.setLevel(logging.INFO) | |
| # Prevent adding multiple handlers if setup is called multiple times | |
| if logger.hasHandlers(): | |
| logger.handlers.clear() | |
| logger.addHandler(log_handler) | |
| return logger | |
| # Create a singleton logger instance | |
| logger = setup_logger() | |