Spaces:
Build error
Build error
File size: 1,657 Bytes
5e56bcf |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import logging
import json
import time
import sys
from datetime import datetime, timezone
class JSONFormatter(logging.Formatter):
"""
Formatter that outputs JSON strings for structured logging.
Compatible with SIEM tools (Splunk, ELK, Datadog).
"""
def format(self, record):
log_obj = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
"line": record.lineno,
}
# Merge extra fields if present
if hasattr(record, "props"):
log_obj.update(record.props)
# Add exception info if present
if record.exc_info:
log_obj["exception"] = self.formatException(record.exc_info)
return json.dumps(log_obj)
def setup_logger(name: str) -> logging.Logger:
"""
Configures a professional logger with JSON formatting.
"""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# Prevent duplicate handlers if called multiple times
if not logger.handlers:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
return logger
def log_audit(logger: logging.Logger, action: str, details: dict):
"""
Helper for Security Audit Logs.
"""
extra = {
"props": {
"event_type": "SECURITY_AUDIT",
"action": action,
**details
}
}
logger.info(action, extra=extra)
|