|
|
@@ -64,3 +64,91 @@ TEST(JsonLayoutTest, CompactMeansNoWhitespace) {
|
|
|
using testing::Eq;
|
|
|
EXPECT_THAT(ss.str(), Eq(compact_output));
|
|
|
}
|
|
|
+
|
|
|
+TEST(JsonLayoutTest, EOLPropertyAppendsNewline) {
|
|
|
+ using namespace logging;
|
|
|
+ using namespace logging::property;
|
|
|
+ properties const props{_obj({
|
|
|
+ {"eventEol", _v(true)}
|
|
|
+ })};
|
|
|
+ auto playout = layouts::instance().get("JsonLayout", props);
|
|
|
+
|
|
|
+ std::stringstream ss;
|
|
|
+ playout->format(ss, {{NOW, 123456}, level::warning, {}, "UnitTest",
|
|
|
+ "This is a test message"});
|
|
|
+
|
|
|
+ using testing::EndsWith;
|
|
|
+ using testing::StartsWith;
|
|
|
+ EXPECT_THAT(ss.str(), StartsWith(formatted_output));
|
|
|
+ EXPECT_THAT(ss.str(), EndsWith(logging::NEWLINE_TOKEN));
|
|
|
+}
|
|
|
+
|
|
|
+std::string const struct_output = R"({
|
|
|
+ "instant": {
|
|
|
+ "epochSecond": 1554401840,
|
|
|
+ "nanoOfSecond": 123456000
|
|
|
+ },
|
|
|
+ "level": "warning",
|
|
|
+ "loggerName": "UnitTest",
|
|
|
+ "message": "{225}"
|
|
|
+})";
|
|
|
+
|
|
|
+struct example {
|
|
|
+ int content;
|
|
|
+};
|
|
|
+std::ostream & operator<<(std::ostream & os, example const & ex) {
|
|
|
+ return os << '{' << ex.content << '}';
|
|
|
+}
|
|
|
+
|
|
|
+std::string const struct_json_output = R"({
|
|
|
+"instant": {
|
|
|
+ "epochSecond": 1554401840,
|
|
|
+ "nanoOfSecond": 123456000
|
|
|
+},
|
|
|
+"level": "warning",
|
|
|
+"loggerName": "UnitTest",
|
|
|
+"message": {
|
|
|
+ "content":225
|
|
|
+}
|
|
|
+})";
|
|
|
+
|
|
|
+struct json_example {
|
|
|
+ int content;
|
|
|
+};
|
|
|
+std::ostream & operator<<(std::ostream & os, json_example const & ex) {
|
|
|
+ return os << '{' << ex.content << '}';
|
|
|
+}
|
|
|
+
|
|
|
+TEST(JsonLayoutTest, MessageCanBeLoggedAsJSON) {
|
|
|
+ using namespace logging;
|
|
|
+ using namespace logging::property;
|
|
|
+ properties const props{_obj({
|
|
|
+ {"objectMessageAsJsonObject", _v(true)}
|
|
|
+ })};
|
|
|
+ auto playout = layouts::instance().get("JsonLayout", props);
|
|
|
+
|
|
|
+ std::stringstream ss;
|
|
|
+ json_example ex{225};
|
|
|
+ playout->format(ss, {{NOW, 123456}, level::warning, {}, "UnitTest",
|
|
|
+ {"This is a test message", ex}});
|
|
|
+
|
|
|
+ using testing::Eq;
|
|
|
+ EXPECT_THAT(ss.str(), Eq(struct_json_output));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(JsonLayoutTest, ObjectLackingJsonDefaultsToString) {
|
|
|
+ using namespace logging;
|
|
|
+ using namespace logging::property;
|
|
|
+ properties const props{_obj({
|
|
|
+ {"objectMessageAsJsonObject", _v(true)}
|
|
|
+ })};
|
|
|
+ auto playout = layouts::instance().get("JsonLayout", props);
|
|
|
+
|
|
|
+ std::stringstream ss;
|
|
|
+ example ex{225};
|
|
|
+ playout->format(ss, {{NOW, 123456}, level::warning, {}, "UnitTest",
|
|
|
+ {"This is a test message", ex}});
|
|
|
+
|
|
|
+ using testing::Eq;
|
|
|
+ EXPECT_THAT(ss.str(), Eq(struct_output));
|
|
|
+}
|