firecracker_http_client/
logger.rs1use crate::validation::validate_writable_path;
2use crate::FirecrackerError;
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use validator::Validate;
6
7#[derive(Debug, Serialize, Deserialize, Validate)]
8pub struct Logger {
9 #[validate(custom = "validate_writable_path")]
10 pub log_path: String,
11 #[serde(skip_serializing_if = "Option::is_none")]
12 #[validate(regex(
13 path = "LOG_LEVEL_REGEX",
14 message = "Invalid log level. Must be one of: Error, Warning, Info, Debug"
15 ))]
16 pub level: Option<String>,
17 #[serde(skip_serializing_if = "Option::is_none")]
18 pub show_level: Option<bool>,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub show_log_origin: Option<bool>,
21}
22
23lazy_static::lazy_static! {
24 static ref LOG_LEVEL_REGEX: regex::Regex = regex::Regex::new(r"^(Error|Warning|Info|Debug)$").unwrap();
25}
26
27#[async_trait]
28pub trait LoggerOperations {
29 async fn put_logger(&self, logger: &Logger) -> Result<(), FirecrackerError>;
30}
31
32#[async_trait]
33impl LoggerOperations for crate::FirecrackerClient {
34 async fn put_logger(&self, logger: &Logger) -> Result<(), FirecrackerError> {
35 logger.validate()?;
36
37 let url = self.url("logger")?;
38 let response = self.client.put(url).json(logger).send().await?;
39
40 if !response.status().is_success() {
41 return Err(FirecrackerError::Api {
42 status_code: response.status().as_u16(),
43 message: response.text().await?,
44 });
45 }
46
47 Ok(())
48 }
49}