Skip to content

Commit db4b78c

Browse files
committed
added the /logs api
1 parent 656f989 commit db4b78c

4 files changed

Lines changed: 166 additions & 3 deletions

File tree

.code-samples.meilisearch.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,3 +2073,23 @@ webhooks_patch_1: |-
20732073
.unwrap();
20742074
webhooks_delete_1: |-
20752075
client.delete_webhook("WEBHOOK_UUID").await.unwrap();
2076+
customize_log_levels_1: |-
2077+
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2078+
let new_log_level = NewLogLevel { target:"info".to_string() };
2079+
client.customize_log_levels(new_log_level).await.unwrap();
2080+
open_log_stream_1: |-
2081+
use futures::StreamExt;
2082+
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2083+
let logs_config = LogStreamRequest {
2084+
target: "info".to_string(),
2085+
mode: LogMode::Human
2086+
};
2087+
let byte_stream = client.open_log_stream(logs_config).await.unwrap();
2088+
byte_s.for_each(|chunk| async {
2089+
if let Ok(chunk) = chunk {
2090+
println!("{}", String::from_utf8_lossy(&chunk));
2091+
}
2092+
}).await;
2093+
interrupt_log_stream_1: |-
2094+
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2095+
client.interrupt_log_stream().await.unwrap();

src/client.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde_json::{json, Value};
44
use std::{collections::HashMap, time::Duration};
55
use time::OffsetDateTime;
66

7+
use crate::logs::NewLogLevel;
78
use crate::{
89
errors::*,
910
indexes::*,
@@ -1444,6 +1445,62 @@ impl<Http: HttpClient> Client<Http> {
14441445
.await
14451446
}
14461447

1448+
/// Customize logging levels for the default logging system.
1449+
///
1450+
///
1451+
/// # Example
1452+
///
1453+
/// ```
1454+
/// # use meilisearch_sdk::{client::*, logs::*};
1455+
/// #
1456+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1457+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1458+
/// #
1459+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1460+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1461+
/// let new_log_level = NewLogLevel { target:"info".to_string() };
1462+
/// client.customize_log_levels(new_log_level).await.unwrap();
1463+
///# });
1464+
/// ```
1465+
pub async fn customize_log_levels(&self, log_target: NewLogLevel) -> Result<(), Error> {
1466+
self.http_client
1467+
.request::<(), NewLogLevel, ()>(
1468+
&format!("{}/logs/stderr", self.host),
1469+
Method::Post {
1470+
query: (),
1471+
body: log_target,
1472+
},
1473+
204,
1474+
)
1475+
.await
1476+
}
1477+
1478+
/// Interrupt a log stream.
1479+
///
1480+
///
1481+
/// # Example
1482+
///
1483+
/// ```
1484+
/// # use meilisearch_sdk::{client::*, logs::*};
1485+
/// #
1486+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1487+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1488+
/// #
1489+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1490+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1491+
/// client.interrupt_log_stream().await.unwrap();
1492+
///# });
1493+
/// ```
1494+
pub async fn interrupt_log_stream(&self) -> Result<(), Error> {
1495+
self.http_client
1496+
.request::<(), (), ()>(
1497+
&format!("{}/logs/stream", self.host),
1498+
Method::Delete { query: () },
1499+
204,
1500+
)
1501+
.await
1502+
}
1503+
14471504
fn sleep_backend(&self) -> SleepBackend {
14481505
SleepBackend::infer(self.http_client.is_tokio())
14491506
}

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,13 @@ pub mod features;
248248
pub mod indexes;
249249
/// Module containing the [`Key`](key::Key) struct.
250250
pub mod key;
251+
/// Module for logs related operations
252+
pub mod logs;
251253
/// Module for Network configuration API (sharding/remotes).
252254
pub mod network;
253255
pub mod request;
256+
#[cfg(feature = "reqwest")]
257+
pub mod reqwest;
254258
/// Module related to search queries and results.
255259
pub mod search;
256260
/// Module containing [`Settings`](settings::Settings).
@@ -271,9 +275,6 @@ mod utils;
271275
/// Module to manage webhooks.
272276
pub mod webhooks;
273277

274-
#[cfg(feature = "reqwest")]
275-
pub mod reqwest;
276-
277278
#[cfg(feature = "reqwest")]
278279
pub type DefaultHttpClient = reqwest::ReqwestClient;
279280

src/logs.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::client::Client;
2+
use crate::errors::Error;
3+
use bytes::Bytes;
4+
use futures_core::Stream;
5+
use serde::Serialize;
6+
7+
#[derive(Serialize)]
8+
pub struct NewLogLevel {
9+
pub target: String,
10+
}
11+
12+
#[derive(Serialize)]
13+
pub struct LogStreamRequest {
14+
pub target: String,
15+
pub mode: LogMode,
16+
}
17+
18+
#[derive(Debug, Default, Clone, Copy, Serialize)]
19+
#[serde(rename_all = "camelCase")]
20+
pub enum LogMode {
21+
/// Output the logs in a human-readable form
22+
#[default]
23+
Human,
24+
/// Output the logs in JSON format
25+
Json,
26+
/// Output the logs in Firefox profiler format for visualization
27+
Profile,
28+
}
29+
30+
impl Client<crate::reqwest::ReqwestClient> {
31+
/// Opens a continuous stream of logs for focused debugging sessions.
32+
/// The stream will continue to run indefinitely until you
33+
/// [interrupt](Client::interrupt_log_stream) it.
34+
///
35+
/// The function returns a [`Stream`] that you can iterate over with
36+
/// the [`StreamExt`] trait to process the logs.
37+
///
38+
/// # Example
39+
///
40+
/// ```
41+
/// # use meilisearch_sdk::{client::*, logs::*};
42+
/// #
43+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
44+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
45+
/// #
46+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
47+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
48+
/// let logs_config = LogStreamRequest {
49+
/// target: "info".to_string(),
50+
/// mode: LogMode::Human
51+
/// };
52+
/// let byte_stream = client.open_log_stream(logs_config).await.unwrap();
53+
///# });
54+
/// ```
55+
pub async fn open_log_stream(
56+
&self,
57+
get_logs: LogStreamRequest,
58+
) -> Result<impl Stream<Item = Result<Bytes, reqwest::Error>>, Error> {
59+
let res = self
60+
.http_client
61+
.inner()
62+
.post(format!("{}/logs/stream", self.host))
63+
.header("Content-Type", "application/json")
64+
.body(serde_json::to_string(&get_logs)?)
65+
.send()
66+
.await?;
67+
res.error_for_status_ref()?;
68+
Ok(res.bytes_stream())
69+
}
70+
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use super::*;
75+
use meilisearch_test_macro::meilisearch_test;
76+
#[meilisearch_test]
77+
async fn test_open_log_stream(client: Client) {
78+
let logs_config = LogStreamRequest {
79+
mode: LogMode::Human,
80+
target: "info".to_string(),
81+
};
82+
assert!(client.open_log_stream(logs_config).await.is_ok());
83+
assert!(client.interrupt_log_stream().await.is_ok());
84+
}
85+
}

0 commit comments

Comments
 (0)