Skip to content

Commit

Permalink
Truncate regex match failures at an arbitrary 60 characters long
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceg committed Jul 10, 2019
1 parent 45786ad commit 615d779
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/transforms/regex_parser.rs
Expand Up @@ -3,6 +3,7 @@ use crate::event::{self, Event, ValueKind};
use crate::types::{parse_conversion_map, Conversion};
use regex::bytes::{CaptureLocations, Regex};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::str;
use string_cache::DefaultAtom as Atom;
Expand Down Expand Up @@ -120,7 +121,7 @@ impl Transform for RegexParser {
} else {
warn!(
message = "Regex pattern failed to match",
field = &String::from_utf8_lossy(&value)[..],
field = &truncate_string_at(&String::from_utf8_lossy(&value), 60)[..],
);
}
} else {
Expand All @@ -138,6 +139,14 @@ impl Transform for RegexParser {
}
}

fn truncate_string_at<'a>(s: &'a str, maxlen: usize) -> Cow<'a, str> {
if s.len() >= maxlen {
format!("{}[...]", &s[..maxlen - 5]).into()
} else {
s.into()
}
}

#[cfg(test)]
mod tests {
use super::RegexParserConfig;
Expand Down

0 comments on commit 615d779

Please sign in to comment.