Skip to content

Commit

Permalink
Tolerate garbage preceding logfmt
Browse files Browse the repository at this point in the history
Accommodates Rails's default tagged logger.

Nom doesn't support dropping the input[^1], resulting in a useless
vector being allocated for the duration of the parse.

[^1]: rust-bakery/nom#1594
  • Loading branch information
clowder committed Apr 21, 2023
1 parent acf5325 commit 24ea746
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::collections::HashMap;
use nom::{
branch::alt,
bytes::complete::{escaped_transform, tag, take_while1},
character::complete::{none_of, one_of, space0},
combinator::{eof, opt},
multi::fold_many1,
character::complete::{anychar, none_of, one_of, space0},
combinator::{eof, opt, peek},
multi::{fold_many1, many_till},
sequence::{delimited, terminated, tuple},
IResult,
};
Expand Down Expand Up @@ -49,7 +49,9 @@ fn pairs(input: &str) -> IResult<&str, HashMap<String, Option<String>>> {
}

pub fn parse(message: &str) -> Option<HashMap<String, Option<String>>> {
pairs(message).map(|(_, result)| result).ok()
tuple((many_till(anychar, peek(pair)), pairs))(message)
.map(|(_rest, (_garbage, result))| result)
.ok()
}

#[cfg(test)]
Expand Down Expand Up @@ -98,6 +100,27 @@ mod tests {
);
}

#[test]
fn test_lograge_lines_with_rails_tagged_prefix() {
assert_eq!(
Some(HashMap::from([
pair("at", Some("info")),
pair("method", Some("POST")),
pair("path", Some("/foo/bar")),
pair("host", Some("example.com")),
pair("request_id", Some("f116113c-b8ed-41ea-bbf3-a031313dd936")),
pair("fwd", Some("0.0.0.0")),
pair("dyno", Some("web.1")),
pair("connect", Some("0ms")),
pair("service", Some("25ms")),
pair("status", Some("204")),
pair("bytes", Some("490")),
pair("protocol", Some("http")),
])),
parse("I, [2023-04-21T16:10:13.841327 #41] INFO -- : at=info method=POST path=\"/foo/bar\" host=example.com request_id=f116113c-b8ed-41ea-bbf3-a031313dd936 fwd=\"0.0.0.0\" dyno=web.1 connect=0ms service=25ms status=204 bytes=490 protocol=http")
);
}

#[test]
fn test_edge_cases() {
// leading whitespace is discarded
Expand Down

0 comments on commit 24ea746

Please sign in to comment.