From 64b635d9923db65db7a15042e6afaa07928a61af Mon Sep 17 00:00:00 2001 From: Artem Medvedev Date: Thu, 18 Jul 2024 02:01:55 +0200 Subject: [PATCH] fix(LoggingConsumer): trim newline char from the mirrored messages (#699) --- .../src/core/logs/consumer/logging_consumer.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/testcontainers/src/core/logs/consumer/logging_consumer.rs b/testcontainers/src/core/logs/consumer/logging_consumer.rs index 3156c06f..b9216f1c 100644 --- a/testcontainers/src/core/logs/consumer/logging_consumer.rs +++ b/testcontainers/src/core/logs/consumer/logging_consumer.rs @@ -42,14 +42,14 @@ impl LoggingConsumer { self } - fn format_message<'a>(&self, message: &'a bytes::Bytes) -> Cow<'a, str> { + fn format_message<'a>(&self, message: &'a str) -> Cow<'a, str> { // Remove trailing newlines - let message = String::from_utf8_lossy(message); + let message = message.trim_end_matches(|c| c == '\n' || c == '\r'); if let Some(prefix) = &self.prefix { Cow::Owned(format!("{} {}", prefix, message)) } else { - message + Cow::Borrowed(message) } } } @@ -65,10 +65,18 @@ impl LogConsumer for LoggingConsumer { async move { match record { LogFrame::StdOut(bytes) => { - log::log!(self.stdout_level, "{}", self.format_message(bytes)); + log::log!( + self.stdout_level, + "{}", + self.format_message(&String::from_utf8_lossy(bytes)) + ); } LogFrame::StdErr(bytes) => { - log::log!(self.stderr_level, "{}", self.format_message(bytes)); + log::log!( + self.stderr_level, + "{}", + self.format_message(&String::from_utf8_lossy(bytes)) + ); } } }