Download core/audit_logger.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/audit_logger.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/core/audit_logger.py
-
curl -L -o audit_logger.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/core/audit_logger.py
3.61 kB
| """ | |
| Enhanced Audit Logger - Rich logging for engineering actions | |
| """ | |
| import sqlite3 | |
| import json | |
| import logging | |
| from pathlib import Path | |
| from datetime import datetime | |
| from typing import Optional, Dict, Any | |
| import pandas as pd | |
| logger = logging.getLogger("track_design") | |
| def get_logger(name: str = "track_design") -> logging.Logger: | |
| """Shared application logger (use instead of print).""" | |
| log = logging.getLogger(name) | |
| if not log.handlers: | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s %(levelname)s [%(name)s] %(message)s", | |
| ) | |
| return log | |
| AUDIT_DB_PATH = Path(__file__).parent.parent / "data" / "audit_log.db" | |
| def init_audit_db(): | |
| """Initialize audit database with enhanced schema""" | |
| AUDIT_DB_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| with sqlite3.connect(AUDIT_DB_PATH) as con: | |
| con.execute(""" | |
| CREATE TABLE IF NOT EXISTS audit_log ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| timestamp TEXT, | |
| user TEXT, | |
| project TEXT, | |
| action TEXT, | |
| details TEXT, | |
| result TEXT, | |
| warnings TEXT, | |
| files_used TEXT, | |
| version INTEGER | |
| ) | |
| """) | |
| con.execute(""" | |
| CREATE TABLE IF NOT EXISTS snapshots ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| timestamp TEXT, | |
| version INTEGER, | |
| engineering_data_json TEXT, | |
| user TEXT, | |
| project TEXT | |
| ) | |
| """) | |
| con.commit() | |
| def log_audit( | |
| action: str, | |
| details: str, | |
| version: int, | |
| user: str = "Engineer", | |
| project: str = "Unknown", | |
| result: str = "SUCCESS", | |
| warnings: str = "", | |
| files_used: str = "" | |
| ): | |
| """Log an audit entry with rich context""" | |
| init_audit_db() | |
| with sqlite3.connect(AUDIT_DB_PATH) as con: | |
| con.execute( | |
| """INSERT INTO audit_log | |
| (timestamp, user, project, action, details, result, warnings, files_used, version) | |
| VALUES (?,?,?,?,?,?,?,?,?)""", | |
| ( | |
| datetime.now().isoformat(), | |
| user, | |
| project, | |
| action, | |
| details, | |
| result, | |
| warnings, | |
| files_used, | |
| version | |
| ) | |
| ) | |
| con.commit() | |
| def create_snapshot(version: int, eng_data_json: str, user: str = "Engineer", project: str = "Unknown") -> int: | |
| """Create a snapshot with metadata""" | |
| init_audit_db() | |
| with sqlite3.connect(AUDIT_DB_PATH) as con: | |
| cur = con.execute( | |
| """INSERT INTO snapshots | |
| (timestamp, version, engineering_data_json, user, project) | |
| VALUES (?,?,?,?,?)""", | |
| (datetime.now().isoformat(), version, eng_data_json, user, project) | |
| ) | |
| con.commit() | |
| return cur.lastrowid | |
| def get_audit_log(limit: int = 100, project: Optional[str] = None) -> pd.DataFrame: | |
| """Get audit log with optional project filter""" | |
| with sqlite3.connect(AUDIT_DB_PATH) as con: | |
| if project: | |
| return pd.read_sql_query( | |
| """SELECT * FROM audit_log | |
| WHERE project = ? | |
| ORDER BY timestamp DESC LIMIT ?""", | |
| con, params=(project, limit) | |
| ) | |
| return pd.read_sql_query( | |
| "SELECT * FROM audit_log ORDER BY timestamp DESC LIMIT ?", | |
| con, params=(limit,) | |
| ) | |