momento_functions_host/
logging.rs1use momento_functions_wit::host::momento::host::logging;
4use thiserror::Error;
5
6pub enum LogDestination {
8 Topic {
10 topic: String,
12 },
13 CloudWatch {
15 iam_role_arn: String,
17 log_group_name: String,
20 },
21}
22
23impl LogDestination {
24 pub fn topic(name: impl Into<String>) -> Self {
26 Self::Topic { topic: name.into() }
27 }
28 pub fn cloudwatch(iam_role_arn: impl Into<String>, log_group_name: impl Into<String>) -> Self {
32 Self::CloudWatch {
33 iam_role_arn: iam_role_arn.into(),
34 log_group_name: log_group_name.into(),
35 }
36 }
37}
38
39pub struct LogConfiguration {
41 log_level: log::LevelFilter,
43 system_log_level: log::LevelFilter,
45 destination: LogDestination,
47}
48
49impl LogConfiguration {
50 pub fn new(destination: LogDestination) -> Self {
52 Self {
53 log_level: log::LevelFilter::Info,
54 system_log_level: log::LevelFilter::Info,
55 destination,
56 }
57 }
58
59 pub fn with_log_level(mut self, log_level: log::LevelFilter) -> Self {
61 self.log_level = log_level;
62 self
63 }
64
65 pub fn with_system_log_level(mut self, system_log_level: log::LevelFilter) -> Self {
67 self.system_log_level = system_log_level;
68 self
69 }
70}
71
72impl From<LogDestination> for LogConfiguration {
73 fn from(value: LogDestination) -> Self {
74 match value {
75 LogDestination::Topic { topic } => Self::new(LogDestination::topic(topic)),
76 LogDestination::CloudWatch {
77 iam_role_arn,
78 log_group_name,
79 } => Self::new(LogDestination::cloudwatch(iam_role_arn, log_group_name)),
80 }
81 }
82}
83
84pub fn log_configuration(destination: LogDestination) -> LogConfiguration {
86 LogConfiguration::new(destination)
87}
88
89impl From<LogDestination> for logging::Destination {
90 fn from(value: LogDestination) -> Self {
91 match value {
92 LogDestination::Topic { topic } => {
93 momento_functions_wit::host::momento::host::logging::Destination::Topic(
94 logging::TopicDestination { topic_name: topic },
95 )
96 }
97 LogDestination::CloudWatch {
98 iam_role_arn,
99 log_group_name,
100 } => momento_functions_wit::host::momento::host::logging::Destination::Cloudwatch(
101 logging::CloudwatchDestination {
102 iam_role_arn,
103 log_group_name,
104 },
105 ),
106 }
107 }
108}
109
110impl From<LogConfiguration> for logging::ConfigureLoggingInput {
111 fn from(value: LogConfiguration) -> Self {
112 Self {
113 log_level: match value.log_level {
114 log::LevelFilter::Off => logging::LogLevel::Off,
115 log::LevelFilter::Error => logging::LogLevel::Error,
116 log::LevelFilter::Warn => logging::LogLevel::Warn,
117 log::LevelFilter::Info => logging::LogLevel::Info,
118 log::LevelFilter::Debug => logging::LogLevel::Debug,
119 log::LevelFilter::Trace => logging::LogLevel::Debug,
121 },
122 system_logs_level: match value.system_log_level {
123 log::LevelFilter::Off => logging::LogLevel::Off,
124 log::LevelFilter::Error => logging::LogLevel::Error,
125 log::LevelFilter::Warn => logging::LogLevel::Warn,
126 log::LevelFilter::Info => logging::LogLevel::Info,
127 log::LevelFilter::Debug => logging::LogLevel::Debug,
128 log::LevelFilter::Trace => logging::LogLevel::Debug,
130 },
131 destination: value.destination.into(),
132 }
133 }
134}
135
136#[derive(Debug, Error)]
138pub enum LogConfigurationError {
139 #[error("Invalid auth provided for configuration! {message}")]
140 Auth {
142 message: String,
144 },
145 #[error("Something went wrong while trying to configure logs! {message}")]
146 Unknown {
148 message: String,
150 },
151}
152
153impl From<logging::LogConfigurationError> for LogConfigurationError {
154 fn from(value: logging::LogConfigurationError) -> Self {
155 match value {
156 logging::LogConfigurationError::Auth(e) => Self::Auth { message: e },
157 }
158 }
159}
160
161pub fn configure_host_logging(
163 configurations: impl IntoIterator<Item = LogConfiguration>,
164) -> Result<(), LogConfigurationError> {
165 let configurations = configurations
166 .into_iter()
167 .map(|configuration| configuration.into())
168 .collect::<Vec<logging::ConfigureLoggingInput>>();
169 Ok(logging::configure_logging(&configurations)?)
170}
171
172pub fn log(input: &str, level: log::Level) {
174 logging::log(
175 input,
176 match level {
177 log::Level::Error => logging::LogLevel::Error,
178 log::Level::Warn => logging::LogLevel::Warn,
179 log::Level::Info => logging::LogLevel::Info,
180 log::Level::Debug => logging::LogLevel::Debug,
181 log::Level::Trace => logging::LogLevel::Debug,
183 },
184 )
185}