turul_mcp_builders/traits/
logging_traits.rs1use turul_mcp_protocol::logging::{LoggingLevel, LoggingMessageNotification, SetLevelRequest};
6use serde_json::Value;
7
8pub trait HasLoggingMetadata {
10 fn method(&self) -> &str;
12
13 fn logger_name(&self) -> Option<&str> {
15 None
16 }
17}
18
19pub trait HasLogLevel {
21 fn level(&self) -> LoggingLevel;
23
24 fn should_log(&self, message_level: LoggingLevel) -> bool {
26 message_level.should_log(self.level())
27 }
28}
29
30pub trait HasLogFormat {
32 fn data(&self) -> &Value;
34
35 fn format_message(&self) -> String {
37 match self.data() {
39 Value::String(s) => s.clone(),
40 other => {
41 serde_json::to_string(other).unwrap_or_else(|_| "<invalid log data>".to_string())
42 }
43 }
44 }
45}
46
47pub trait HasLogTransport {
49 fn should_deliver(&self, _level: LoggingLevel) -> bool {
51 true
52 }
53
54 fn batch_size(&self) -> Option<usize> {
56 None
57 }
58}
59
60pub trait LoggerDefinition:
66 HasLoggingMetadata + HasLogLevel + HasLogFormat + HasLogTransport
67{
68 fn to_message_notification(&self) -> LoggingMessageNotification {
70 let mut notification = LoggingMessageNotification::new(self.level(), self.data().clone());
71 if let Some(logger) = self.logger_name() {
72 notification = notification.with_logger(logger);
73 }
74 notification
75 }
76
77 fn to_set_level_request(&self) -> SetLevelRequest {
79 SetLevelRequest::new(self.level())
80 }
81}
82
83impl<T> LoggerDefinition for T where
85 T: HasLoggingMetadata + HasLogLevel + HasLogFormat + HasLogTransport
86{
87}