pulseengine_mcp_logging/lib.rs
1//! Structured logging framework for MCP servers
2//!
3//! This crate provides comprehensive logging capabilities for MCP servers including:
4//! - Structured logging with tracing
5//! - Metrics collection and reporting
6//! - Log sanitization for security
7//! - Performance monitoring integration
8//!
9//! # Example
10//!
11//! ```rust,no_run
12//! use mcp_logging::{MetricsCollector, StructuredLogger};
13//!
14//! #[tokio::main]
15//! async fn main() {
16//! let metrics = MetricsCollector::new();
17//! let logger = StructuredLogger::new();
18//!
19//! // Initialize structured logging
20//! logger.init().expect("Failed to initialize logging");
21//!
22//! // Log with structured context
23//! tracing::info!("Server started", server_type = "mcp", version = "1.0");
24//! }
25//! ```
26
27pub mod metrics;
28pub mod sanitization;
29pub mod structured;
30
31// Re-export main types for convenience
32pub use metrics::{
33 get_metrics, BusinessMetrics, ErrorMetrics, ErrorRecord, HealthMetrics, MetricsCollector,
34 MetricsSnapshot, RequestMetrics,
35};
36pub use sanitization::{LogSanitizer, SanitizationConfig};
37pub use structured::{ErrorClass, StructuredContext, StructuredLogger};
38
39/// Result type for logging operations
40pub type Result<T> = std::result::Result<T, LoggingError>;
41
42/// Logging error types
43#[derive(Debug, thiserror::Error)]
44pub enum LoggingError {
45 #[error("Configuration error: {0}")]
46 Config(String),
47
48 #[error("I/O error: {0}")]
49 Io(#[from] std::io::Error),
50
51 #[error("Serialization error: {0}")]
52 Serialization(#[from] serde_json::Error),
53
54 #[error("Tracing error: {0}")]
55 Tracing(String),
56}
57
58/// Generic error trait for classification
59pub trait ErrorClassification: std::fmt::Display + std::error::Error {
60 fn error_type(&self) -> &str;
61 fn is_retryable(&self) -> bool;
62 fn is_timeout(&self) -> bool;
63 fn is_auth_error(&self) -> bool;
64 fn is_connection_error(&self) -> bool;
65}