Skip to content

Commit

Permalink
access_log: add newline between JSON log lines (#5024)
Browse files Browse the repository at this point in the history
Tack a newline onto the end of all JSON access log lines

Risk Level: Low
Testing: Every JSON unit test will now check to see that there is one (and only one) newline in a log line
Docs Changes: None
Release Notes: None

Fixes #5018

Signed-off-by: Aaltan Ahmad <aa@stripe.com>
  • Loading branch information
aa-stripe authored and htuch committed Nov 14, 2018
1 parent e209121 commit cc991fe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/common/access_log/access_log_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ std::string JsonFormatterImpl::format(const Http::HeaderMap& request_headers,
fmt::format("Error serializing access log to JSON: {}", conversion_status.ToString());
}

return log_line;
return absl::StrCat(log_line, "\n");
}

std::unordered_map<std::string, std::string> JsonFormatterImpl::toMap(
Expand Down
8 changes: 8 additions & 0 deletions test/common/access_log/access_log_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ TEST(AccessLogFormatterTest, startTimeFormatter) {
void verifyJsonOutput(std::string json_string,
std::unordered_map<std::string, std::string> expected_map) {
const auto parsed = Json::Factory::loadFromString(json_string);

// Every json log line should have only one newline character, and it should be the last character
// in the string
const auto newline_pos = json_string.find('\n');
EXPECT_NE(newline_pos, std::string::npos);
EXPECT_EQ(newline_pos, json_string.length() - 1);

for (const auto& pair : expected_map) {
EXPECT_EQ(parsed->getString(pair.first), pair.second);
}
Expand All @@ -411,6 +418,7 @@ TEST(AccessLogFormatterTest, JsonFormatterPlainStringTest) {
verifyJsonOutput(formatter.format(request_header, response_header, response_trailer, stream_info),
expected_json_map);
}

TEST(AccessLogFormatterTest, JsonFormatterSingleOperatorTest) {
StreamInfo::MockStreamInfo stream_info;
Http::TestHeaderMapImpl request_header;
Expand Down

0 comments on commit cc991fe

Please sign in to comment.