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 just dropping the garbage[^1], it's accumulated in a
newly allocated vector.

[^1]: rust-bakery/nom#1594
  • Loading branch information
clowder committed Apr 21, 2023
1 parent acf5325 commit 2a45b7e
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, [2022-08-05T15:55:06.335844 #56] INFO -- : [242dc622-3727-4e5e-ac6e-fcf121a1a532] 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 2a45b7e

Please sign in to comment.