Skip to content

Commit 687e867

Browse files
Initial logging integration
at the 'trace' level. References rabbitmq/rabbitmqadmin-ng#51.
1 parent cf1f4d6 commit 687e867

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## v0.61.0 (in development)
44

5-
No changes yet.
5+
### Enhancements
6+
7+
* Integrated logging with the `log` crate. Both clients now emit trace-level logs for HTTP operations, including request/response details and retry attempts.
68

79
## v0.60.0 (Sep 28, 2025)
810

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tabled = { version = "0.20", features = ["derive", "macros"], optional = true }
2828
regex = { version = "1", features = ["std"] }
2929
url = "2.5"
3030
urlencoding = "2.1.3"
31+
log = "0.4"
3132

3233
reqwest = { version = "0.12", default-features = false, features = [
3334
"json",
@@ -42,6 +43,7 @@ time = { version = "0.3.41", features = ["serde-human-readable"] }
4243
amqprs = { version = "2" }
4344
cargo-nextest = "0.9.104"
4445
proptest = "1.4"
46+
fern = "0.7"
4547

4648
[features]
4749
default = ["blocking", "default-tls"]

src/api/client.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use crate::commons::RetrySettings;
1717
use crate::error::Error::{ClientErrorResponse, NotFound, ServerErrorResponse};
1818
use backtrace::Backtrace;
19+
use log::trace;
1920
use reqwest::{Client as HttpClient, StatusCode, header::HeaderMap};
2021
use serde::Serialize;
2122
use std::fmt;
@@ -211,6 +212,11 @@ where
211212
pub fn new(endpoint: E, username: U, password: P) -> Self {
212213
let client = HttpClient::builder().build().unwrap();
213214

215+
trace!(
216+
"Created new async RabbitMQ HTTP API client for endpoint: {}",
217+
endpoint
218+
);
219+
214220
Self {
215221
endpoint,
216222
username,
@@ -282,8 +288,24 @@ where
282288
let n = self.retry_settings.max_attempts;
283289
for attempt in 0..=n {
284290
match operation().await {
285-
Ok(response) => return Ok(response),
291+
Ok(response) => {
292+
if attempt > 0 {
293+
trace!("Request succeeded after {} retry attempt(s)", attempt);
294+
}
295+
return Ok(response);
296+
}
286297
Err(e) => {
298+
if attempt < n {
299+
trace!(
300+
"Request failed on attempt {}/{}, retrying in {}ms: {}",
301+
attempt + 1,
302+
n + 1,
303+
self.retry_settings.delay_ms,
304+
e
305+
);
306+
} else {
307+
trace!("Request failed after {} attempt(s): {}", n + 1, e);
308+
}
287309
last_error = Some(e);
288310

289311
// Don't sleep after the last attempt
@@ -346,6 +368,8 @@ where
346368
let username = self.username.to_string();
347369
let password = self.password.to_string();
348370

371+
trace!("HTTP GET: {}", rooted_path);
372+
349373
self.with_retry(|| async {
350374
let response = self
351375
.client
@@ -378,6 +402,12 @@ where
378402
let username = self.username.to_string();
379403
let password = self.password.to_string();
380404

405+
if let Ok(body) = serde_json::to_string_pretty(payload) {
406+
trace!("HTTP PUT: {}\nRequest body:\n{}", rooted_path, body);
407+
} else {
408+
trace!("HTTP PUT: {}", rooted_path);
409+
}
410+
381411
self.with_retry(|| async {
382412
let response = self
383413
.client
@@ -411,6 +441,12 @@ where
411441
let username = self.username.to_string();
412442
let password = self.password.to_string();
413443

444+
if let Ok(body) = serde_json::to_string_pretty(payload) {
445+
trace!("HTTP POST: {}\nRequest body:\n{}", rooted_path, body);
446+
} else {
447+
trace!("HTTP POST: {}", rooted_path);
448+
}
449+
414450
self.with_retry(|| async {
415451
let response = self
416452
.client
@@ -472,6 +508,8 @@ where
472508
let username = self.username.to_string();
473509
let password = self.password.to_string();
474510

511+
trace!("HTTP DELETE: {}", rooted_path);
512+
475513
self.with_retry(|| async {
476514
let response = self
477515
.client
@@ -533,6 +571,7 @@ where
533571
Some(status_code) if status_code == StatusCode::NOT_FOUND => {}
534572
_ => {
535573
if status == StatusCode::NOT_FOUND {
574+
trace!("Resource not found (404) at {}", response.url());
536575
return Err(NotFound);
537576
}
538577
}
@@ -547,6 +586,7 @@ where
547586
// this consumes `self` and makes the response largely useless to the caller,
548587
// so we copy the key parts into the error first
549588
let body = response.text().await?;
589+
trace!("HTTP API response: {} from {}: {}", status, url, body);
550590
return Err(ClientErrorResponse {
551591
url: Some(url),
552592
body: Some(body),
@@ -567,6 +607,7 @@ where
567607
// this consumes `self` and makes the response largely useless to the caller,
568608
// so we copy the key parts into the error first
569609
let body = response.text().await?;
610+
trace!("HTTP API response: {} from {}: {}", status, url, body);
570611
return Err(ServerErrorResponse {
571612
url: Some(url),
572613
body: Some(body),

src/blocking_api/client.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use crate::commons::RetrySettings;
1717
use crate::error::Error::{ClientErrorResponse, NotFound, ServerErrorResponse};
1818
use backtrace::Backtrace;
19+
use log::trace;
1920
use reqwest::{StatusCode, blocking::Client as HttpClient, header::HeaderMap};
2021
use serde::Serialize;
2122
use std::fmt;
@@ -210,6 +211,11 @@ where
210211
pub fn new(endpoint: E, username: U, password: P) -> Self {
211212
let client = HttpClient::builder().build().unwrap();
212213

214+
trace!(
215+
"Created new blocking RabbitMQ HTTP API client for endpoint: {}",
216+
endpoint
217+
);
218+
213219
Self {
214220
endpoint,
215221
username,
@@ -280,8 +286,24 @@ where
280286
let n = self.retry_settings.max_attempts;
281287
for attempt in 0..=n {
282288
match operation() {
283-
Ok(response) => return Ok(response),
289+
Ok(response) => {
290+
if attempt > 0 {
291+
trace!("Request succeeded after {} retry attempt(s)", attempt);
292+
}
293+
return Ok(response);
294+
}
284295
Err(e) => {
296+
if attempt < n {
297+
trace!(
298+
"Request failed on attempt {}/{}, retrying in {}ms: {}",
299+
attempt + 1,
300+
n + 1,
301+
self.retry_settings.delay_ms,
302+
e
303+
);
304+
} else {
305+
trace!("Request failed after {} attempt(s): {}", n + 1, e);
306+
}
285307
last_error = Some(e);
286308

287309
// Don't sleep after the last attempt
@@ -348,6 +370,8 @@ where
348370
let username = self.username.to_string();
349371
let password = self.password.to_string();
350372

373+
trace!("HTTP GET: {}", rooted_path);
374+
351375
self.with_retry(|| {
352376
let response = self
353377
.client
@@ -377,6 +401,12 @@ where
377401
let username = self.username.to_string();
378402
let password = self.password.to_string();
379403

404+
if let Ok(body) = serde_json::to_string_pretty(payload) {
405+
trace!("HTTP PUT: {}\nRequest body:\n{}", rooted_path, body);
406+
} else {
407+
trace!("HTTP PUT: {}", rooted_path);
408+
}
409+
380410
self.with_retry(|| {
381411
let response = self
382412
.client
@@ -407,6 +437,12 @@ where
407437
let username = self.username.to_string();
408438
let password = self.password.to_string();
409439

440+
if let Ok(body) = serde_json::to_string_pretty(payload) {
441+
trace!("HTTP POST: {}\nRequest body:\n{}", rooted_path, body);
442+
} else {
443+
trace!("HTTP POST: {}", rooted_path);
444+
}
445+
410446
self.with_retry(|| {
411447
let response = self
412448
.client
@@ -462,6 +498,8 @@ where
462498
let username = self.username.to_string();
463499
let password = self.password.to_string();
464500

501+
trace!("HTTP DELETE: {}", rooted_path);
502+
465503
self.with_retry(|| {
466504
let response = self
467505
.client
@@ -517,6 +555,7 @@ where
517555
Some(status_code) if status_code == StatusCode::NOT_FOUND => {}
518556
_ => {
519557
if status == StatusCode::NOT_FOUND {
558+
trace!("Resource not found (404) at {}", response.url());
520559
return Err(NotFound);
521560
}
522561
}
@@ -531,6 +570,7 @@ where
531570
// this consumes `self` and makes the response largely useless to the caller,
532571
// so we copy the key parts into the error first
533572
let body = response.text()?;
573+
trace!("Client error response: {} from {}: {}", status, url, body);
534574
return Err(ClientErrorResponse {
535575
url: Some(url),
536576
body: Some(body),
@@ -551,6 +591,7 @@ where
551591
// this consumes `self` and makes the response largely useless to the caller,
552592
// so we copy the key parts into the error first
553593
let body = response.text()?;
594+
trace!("Server error response: {} from {}: {}", status, url, body);
554595
return Err(ServerErrorResponse {
555596
url: Some(url),
556597
body: Some(body),

0 commit comments

Comments
 (0)