Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access_log: add newline between JSON log lines #5024

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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 fmt::format("{}\n", log_line);
aa-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

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
auto newline_pos = json_string.find('\n');
aa-stripe marked this conversation as resolved.
Show resolved Hide resolved
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