Overview
The Secure MCP Gateway implements structured logging with contextual information for comprehensive debugging, auditing, and monitoring. Logs are exported via OpenTelemetry to Loki for aggregation and analysis in Grafana.Logging Architecture
Log Levels
The gateway supports standard Python logging levels:Detailed diagnostic information for troubleshooting. Use sparingly in production.Examples:
- Cache lookups
- Configuration loading
- Detailed request/response data
General operational events. Default level for production.Examples:
- Tool execution started/completed
- Authentication success
- Server discovery
Unexpected but handled situations that may require attention.Examples:
- Cache misses
- Slow operations (approaching timeout)
- Deprecated API usage
Error conditions that prevented an operation from completing.Examples:
- Tool execution failures
- Authentication failures
- Guardrail API errors
Severe errors requiring immediate attention.Examples:
- Gateway initialization failure
- Critical system resource exhaustion
- Security breach attempts
Configuring Log Level
Set the log level inenkrypt_mcp_config.json:
DEBUG, INFO, WARNING, ERROR, CRITICAL
Structured Logging
Lazy Logger Pattern
The gateway uses a lazy logger to avoid circular imports during initialization: Location:src/secure_mcp_gateway/utils.py:63
Using the Logger
Basic Usage:Log Context Structure
The gateway uses thebuild_log_extra() function to create structured context:
Location: src/secure_mcp_gateway/utils.py:352
Standard Context Fields
Logs include these standard fields when available:| Field | Type | Description |
|---|---|---|
custom_id | string | Request correlation ID (34 chars + timestamp) |
server_name | string | MCP server name |
tool_name | string | Tool being executed |
project_id | string | Project UUID |
project_name | string | Project name |
user_id | string | User UUID |
email | string | User email (masked in sensitive contexts) |
mcp_config_id | string | Configuration UUID |
duration_ms | int | Operation duration in milliseconds |
success | boolean | Operation success status |
error | string | Error message if failed |
error_type | string | Error classification |
Log Aggregation with Loki
Loki Configuration
Location:infra/loki/loki-config.yaml
Collector Export to Loki
Location:infra/otel_collector/otel-collector-config.yaml
Accessing Loki
- API: http://localhost:3100
- Ready Check: http://localhost:3100/ready
- Metrics: http://localhost:3100/metrics
Querying Logs in Grafana
LogQL Basics
Loki uses LogQL for querying logs:Advanced Queries
Accessing Grafana Explore
- Open Grafana: http://localhost:3000
- Navigate to Explore (compass icon)
- Select Loki datasource
- Enter LogQL query
- Click “Run query”
Live Tail
View logs in real-time:- Grafana → Explore
- Select Loki
- Click “Live” button
- Enter query:
{service_name="secure-mcp-gateway"} - Logs stream in real-time
Log Format Examples
Tool Execution Log
Guardrail Violation Log
Error Log
Logging Best Practices
Always Include Context
Always Include Context
Use structured logging with contextual fields:Structured logs enable powerful filtering and analysis.
Use Appropriate Log Levels
Use Appropriate Log Levels
DEBUG: Detailed diagnostic info (cache lookups, config loading)INFO: Normal operations (tool execution, auth success)WARNING: Unexpected but handled (cache miss, slow operation)ERROR: Operation failures (tool error, auth failure)CRITICAL: Severe errors (gateway crash, security breach)
Mask Sensitive Data
Mask Sensitive Data
Always mask sensitive information:The
mask_sensitive_data function masks keys like: token, key, secret, password, auth, etc.Use Correlation IDs
Use Correlation IDs
Always include This enables tracking requests across all logs.
custom_id for request tracing:Log at Decision Points
Log at Decision Points
Log important decisions and branches:
Include Timing Information
Include Timing Information
Log operation durations:
Log Retention and Management
Retention Configuration
Configure retention in Loki:Compaction
Loki automatically compacts old chunks to save space. Configure inloki-config.yaml:
Log Volume Management
Reduce log volume:- Increase log level: Use
WARNINGorERRORin production - Sample logs: Log only a percentage of requests
- Filter before export: Use collector processors to filter low-value logs
Troubleshooting
Logs Not Appearing in Loki
-
Check Loki is running:
-
Verify collector exports to Loki:
-
Check gateway logs are being exported:
-
Test Loki API:
Logs Not Structured
Symptom: Logs appear as plain text instead of JSON Cause: Not usingextra parameter
Solution:
High Log Volume
Symptom: Excessive disk usage, slow queries Solutions:- Increase log level to
WARNING - Reduce
DEBUGlogs in production - Configure log sampling
- Reduce retention period
Cannot Query by Field
Symptom: LogQL queries by field don’t work Cause: Need to parse JSON Solution:Next Steps
Metrics
Explore Prometheus metrics and Grafana dashboards
OpenTelemetry Setup
Configure OTLP export and distributed tracing
Overview
Return to observability overview
Troubleshooting
Common issues and solutions