pub trait LoggerDefinition:
HasLoggingMetadata
+ HasLogLevel
+ HasLogFormat
+ HasLogTransport {
// Provided methods
fn to_message_notification(&self) -> LoggingMessageNotification { ... }
fn to_set_level_request(&self) -> SetLevelRequest { ... }
}
Expand description
Complete MCP Logger Creation - Build structured logging systems with level control.
This trait represents a complete, working MCP logger that can emit structured
log messages with configurable levels, formats, and transport mechanisms. When you implement
the required metadata traits, you automatically get LoggerDefinition
for free
via blanket implementation.
§What You’re Building
A logger is a sophisticated logging system that:
- Emits structured log messages with configurable levels
- Supports different output formats (JSON, text, etc.)
- Routes messages through various transports (console, files, network)
- Provides runtime level control for debugging
§How to Create a Logger
Implement these four traits on your struct:
// This struct will automatically implement LoggerDefinition!
struct ApplicationLogger {
component: String,
}
impl HasLoggingMetadata for ApplicationLogger {
fn method(&self) -> &str {
"notifications/message"
}
fn logger_name(&self) -> Option<&str> {
Some(&self.component)
}
}
impl HasLogLevel for ApplicationLogger {
fn level(&self) -> LogLevel {
LogLevel::Info
}
}
impl HasLogFormat for ApplicationLogger {
fn data(&self) -> &Value {
use serde_json::{json, Value};
static SAMPLE_DATA: &Value = &Value::Null; // Simplified for docs
SAMPLE_DATA
}
}
impl HasLogTransport for ApplicationLogger {
fn should_deliver(&self, level: LoggingLevel) -> bool {
level.priority() >= LoggingLevel::Info.priority()
}
}
// Now you can use it with the server:
let logger = ApplicationLogger { component: "user-service".to_string() };
// The logger automatically implements LoggerDefinition
let notification = logger.to_message_notification();
let level_request = logger.to_set_level_request();
§Key Benefits
- Structured Logging: JSON-based log data for easy parsing
- Level Control: Runtime adjustment of logging verbosity
- Transport Flexibility: Multiple output destinations
- MCP Compliant: Fully compatible with MCP 2025-06-18 specification
§Common Use Cases
- Application component logging
- Debug trace collection
- Audit trail generation
- Performance monitoring
- Error tracking and reporting
Provided Methods§
Sourcefn to_message_notification(&self) -> LoggingMessageNotification
fn to_message_notification(&self) -> LoggingMessageNotification
Convert this logger definition to a LoggingMessageNotification
Sourcefn to_set_level_request(&self) -> SetLevelRequest
fn to_set_level_request(&self) -> SetLevelRequest
Convert this logger definition to a SetLevelRequest