Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 86 additions & 32 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,43 +174,45 @@ impl SealedLog for LogStorage {
if inner.truncated {
return;
}
if let Some(max_lines) = self.max_lines
&& inner.records.len() >= max_lines
{
inner.records.push(StoredRecord {
level: Level::Warn,
message: "too many lines in the log, truncating it".into(),
});
inner.truncated = true;
return;
}
let mut message = record.args().to_string();

if let Some(max_line_length) = self.max_line_length
&& message.len() > max_line_length
{
let mut length = max_line_length - 3;
while !message.is_char_boundary(length) {
length -= 1;

for mut message in record.args().to_string().lines().map(ToOwned::to_owned) {
if let Some(max_lines) = self.max_lines
&& inner.records.len() >= max_lines
{
inner.records.push(StoredRecord {
level: Level::Warn,
message: "too many lines in the log, truncating it".into(),
});
inner.truncated = true;
return;
}
message = format!("{}...", &message[..length]);
}

if let Some(max_size) = self.max_size
&& inner.size + message.len() >= max_size
{
if let Some(max_line_length) = self.max_line_length
&& message.len() > max_line_length
{
let mut length = max_line_length - 3;
while !message.is_char_boundary(length) {
length -= 1;
}
Comment on lines +193 to +196
Copy link
Copy Markdown
Contributor

@Skgland Skgland May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut length = max_line_length - 3;
while !message.is_char_boundary(length) {
length -= 1;
}
let length = message.floor_char_boundary(max_line_length - 3);

Could this use str::floor_char_boundary or is this newer than rustwides msrv?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we currently have 1.89 in there, that method case with 1.91.

I feel like this is not reason enough to push the msrv?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds reasonable to not bump the msrv for just this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also always wondering how many real-world users outside of the rust org actually exist, so if the msrv la really important to them

message = format!("{}...", &message[..length]);
}

if let Some(max_size) = self.max_size
&& inner.size + message.len() >= max_size
{
inner.records.push(StoredRecord {
level: Level::Warn,
message: "too much data in the log, truncating it".into(),
});
inner.truncated = true;
return;
}
inner.size += message.len();
inner.records.push(StoredRecord {
level: Level::Warn,
message: "too much data in the log, truncating it".into(),
level: record.level(),
message,
});
inner.truncated = true;
return;
}
inner.size += message.len();
inner.records.push(StoredRecord {
level: record.level(),
message,
});
}

fn flush(&self) {}
Expand Down Expand Up @@ -376,6 +378,58 @@ mod tests {
);
}

#[test]
fn test_multiline_messages_are_split_into_records() {
logging::init();

let storage = LogStorage::new(LevelFilter::Info);
logging::capture(&storage, || {
info!("first\nsecond\nthird");
});

assert_eq!(
storage.inner.lock().unwrap().records,
vec![
StoredRecord {
level: Level::Info,
message: "first".to_string(),
},
StoredRecord {
level: Level::Info,
message: "second".to_string(),
},
StoredRecord {
level: Level::Info,
message: "third".to_string(),
},
]
);
}

#[test]
fn test_multiline_messages_respect_max_lines() {
logging::init();

let mut storage = LogStorage::new(LevelFilter::Info);
storage.set_max_lines(2);
logging::capture(&storage, || {
info!("first\nsecond\nthird");
});

let inner = storage.inner.lock().unwrap();
assert_eq!(inner.records.len(), 3);
assert_eq!(inner.records[0].message, "first");
assert_eq!(inner.records[1].message, "second");
assert!(
inner
.records
.last()
.unwrap()
.message
.contains("too many lines")
);
}

#[test]
fn test_too_long_line() {
logging::init();
Expand Down
Loading