feat: harden gateway with policy engine, secure tools, and governance docs
This commit is contained in:
1 parent
e17d34e6d7
commit
5969892af3
55 files changed
+4711
-1587
No files matched your search
@@ -0,0 +1,48 @@
|
||||
"""Structured logging configuration utilities."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from aegis_gitea_mcp.request_context import get_request_id
|
||||
|
||||
|
||||
class JsonLogFormatter(logging.Formatter):
|
||||
"""Format log records as JSON documents."""
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
"""Serialize a log record to JSON."""
|
||||
payload = {
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"level": record.levelname,
|
||||
"logger": record.name,
|
||||
"message": record.getMessage(),
|
||||
"request_id": get_request_id(),
|
||||
}
|
||||
|
||||
if record.exc_info:
|
||||
# Security decision: include only exception type to avoid stack leakage.
|
||||
exception_type = record.exc_info[0]
|
||||
if exception_type is not None:
|
||||
payload["exception_type"] = str(exception_type.__name__)
|
||||
|
||||
return json.dumps(payload, separators=(",", ":"), ensure_ascii=True)
|
||||
|
||||
|
||||
def configure_logging(level: str) -> None:
|
||||
"""Configure application-wide structured JSON logging.
|
||||
|
||||
Args:
|
||||
level: Logging level string.
|
||||
"""
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(level.upper())
|
||||
|
||||
for handler in list(logger.handlers):
|
||||
logger.removeHandler(handler)
|
||||
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(JsonLogFormatter())
|
||||
logger.addHandler(stream_handler)
|
||||
Reference in new issue
Block a user