pulseengine-mcp-logging
Structured logging framework for MCP servers
This crate provides structured logging specifically designed for MCP servers, with automatic credential sanitization, request correlation, and security-focused log management.
What This Provides
Structured Logging:
- JSON output with consistent field names
- Correlation IDs for tracking requests across components
- Log levels with appropriate filtering
- Contextual information (tool names, client IPs, etc.)
Security Features:
- Automatic credential scrubbing from logs
- Sensitive parameter filtering
- Request sanitization before logging
- Audit trail capabilities
MCP-Specific Features:
- Tool execution logging with parameters
- Protocol message logging (request/response)
- Transport layer activity tracking
- Performance metrics integration
Real-World Usage
This logging system is actively used in the Loxone MCP Server where it:
- Logs all 30+ tool executions with sanitized parameters
- Tracks authentication attempts and API key usage
- Provides audit trails for home automation commands
- Integrates with system monitoring for alerting
- Sanitizes device credentials and API keys from logs
Quick Start
[]
= "0.2.0"
= "0.1"
= "1.0"
Basic Usage
Initialize Logging
use ;
// Configure structured logging
let config = LoggingConfig ;
// Initialize the logging system
init_logging?;
Basic Logging
use ;
use log_tool_execution;
// Standard structured logging
info!;
// MCP-specific logging
log_tool_execution;
Request Correlation
use ;
// Generate correlation ID for a request
let correlation_id = new;
// All logs within this scope will include the correlation ID
with_correlation_id.await;
Current Status
Solid foundation with good security practices. The logging system handles the most important concerns well and integrates cleanly with the rest of the framework.
What works well:
- ✅ Structured JSON logging with consistent format
- ✅ Automatic credential sanitization
- ✅ Request correlation tracking
- ✅ MCP-specific logging utilities
- ✅ Integration with standard Rust logging ecosystem
- ✅ File rotation and output management
Areas for improvement:
- 📊 Better integration with metrics systems
- 🔧 More sophisticated log analysis tools
- 📝 More examples for different deployment scenarios
- 🧪 Testing utilities for log validation
Security and Sanitization
Automatic Credential Scrubbing
use sanitize_for_logging;
// Automatically removes sensitive data
let safe_params = sanitize_for_logging;
info!;
// Logs: {"params": {"username": "admin", "password": "[REDACTED]", "api_key": "[REDACTED]", "device": "living_room_light"}}
Custom Sanitization Rules
use ;
let sanitization_config = SanitizationConfig ;
Security Audit Logging
use audit_log;
// Log security-relevant events
audit_log!;
audit_log!;
MCP-Specific Logging
Tool Execution Logging
use ;
// Start tool execution
let execution_id = log_tool_start;
// Tool execution logic...
match execute_tool.await
Protocol Message Logging
use ;
// Log incoming requests
log_request;
// Log outgoing responses
log_response;
Transport Activity
use log_transport_event;
// Log transport-specific events
log_transport_event!;
log_transport_event!;
Configuration
Log Levels and Filtering
use LoggingConfig;
let config = LoggingConfig ;
Output Destinations
let config = LoggingConfig ;
JSON vs Human-Readable Format
// For production - structured JSON
let prod_config = LoggingConfig ;
// For development - human-readable
let dev_config = LoggingConfig ;
Integration Examples
With MCP Server
use ServerConfig;
use LoggingConfig;
let logging_config = LoggingConfig ;
// Initialize logging before starting server
init_logging?;
let server = new.await?;
server.run.await?;
With Authentication System
use AuthManager;
use audit_log;
// Log authentication events
let auth_result = auth_manager.validate_request.await;
match auth_result
Real-World Examples
Loxone Server Logging
// Log home automation commands
info!;
// Log device status queries
debug!;
// Log security events
audit_log!;
Contributing
Logging is fundamental to operational visibility. Most valuable contributions:
- Security improvements - Better sanitization rules and audit capabilities
- Performance optimization - Low-overhead logging for high-throughput servers
- Integration examples - How to integrate with log aggregation systems
- Analysis tools - Utilities for analyzing MCP server logs
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Repository: https://github.com/avrabe/mcp-loxone