diff options
-rw-r--r-- | src/mongo/logv2/attribute_storage.h | 34 | ||||
-rw-r--r-- | src/mongo/logv2/logv2_test.cpp | 26 |
2 files changed, 58 insertions, 2 deletions
diff --git a/src/mongo/logv2/attribute_storage.h b/src/mongo/logv2/attribute_storage.h index 0c81a57e854..6c03caaa9e5 100644 --- a/src/mongo/logv2/attribute_storage.h +++ b/src/mongo/logv2/attribute_storage.h @@ -34,6 +34,7 @@ #include "mongo/logv2/constants.h" #include "mongo/stdx/type_traits.h" #include "mongo/stdx/variant.h" +#include "mongo/util/assert_util.h" #include "mongo/util/duration.h" #include <boost/container/small_vector.hpp> @@ -380,8 +381,21 @@ public: if constexpr (std::is_same_v<decltype(val), CustomAttributeValue&&>) { if (val.stringSerialize) { val.stringSerialize(buffer); - } else { + } else if (val.toString) { fmt::format_to(buffer, "{}", val.toString()); + } else if (val.BSONSerialize) { + BSONObjBuilder objBuilder; + val.BSONSerialize(objBuilder); + objBuilder.done().jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, 0, false, buffer); + } else if (val.BSONAppend) { + BSONObjBuilder objBuilder; + val.BSONAppend(objBuilder, ""_sd); + objBuilder.done().getField(""_sd).jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, false, false, 0, buffer); + } else { + val.toBSONArray().jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, 0, true, buffer); } } else if constexpr (isDuration<std::decay_t<decltype(val)>>) { @@ -481,8 +495,24 @@ public: if (val.stringSerialize) { fmt::format_to(buffer, "{}: ", key); val.stringSerialize(buffer); - } else { + } else if (val.toString) { fmt::format_to(buffer, "{}: {}", key, val.toString()); + } else if (val.BSONSerialize) { + BSONObjBuilder objBuilder; + val.BSONSerialize(objBuilder); + fmt::format_to(buffer, "{}: ", key); + objBuilder.done().jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, 0, false, buffer); + } else if (val.BSONAppend) { + BSONObjBuilder objBuilder; + val.BSONAppend(objBuilder, ""_sd); + fmt::format_to(buffer, "{}: ", key); + objBuilder.done().getField(""_sd).jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, false, false, 0, buffer); + } else { + fmt::format_to(buffer, "{}: ", key); + val.toBSONArray().jsonStringBuffer( + JsonStringFormat::ExtendedRelaxedV2_0_0, 0, true, buffer); } } else if constexpr (isDuration<std::decay_t<decltype(val)>>) { fmt::format_to(buffer, "{}: {}", key, val.toString()); diff --git a/src/mongo/logv2/logv2_test.cpp b/src/mongo/logv2/logv2_test.cpp index 20884459b06..ac1ab0d25ea 100644 --- a/src/mongo/logv2/logv2_test.cpp +++ b/src/mongo/logv2/logv2_test.cpp @@ -1814,5 +1814,31 @@ TEST_F(UnstructuredLoggingTest, UserBothStringAndBSON) { }); } +TEST_F(UnstructuredLoggingTest, VectorBSON) { + std::vector<BSONObj> vectorBSON = {BSON("str1" + << "str2"), + BSON("str3" + << "str4")}; + logd("{}", vectorBSON); // NOLINT + validate([&vectorBSON](const BSONObj& obj) { + ASSERT_EQUALS(obj.getField(kMessageFieldName).String(), + "({\"str1\":\"str2\"}, {\"str3\":\"str4\"})"); + }); +} + +TEST_F(UnstructuredLoggingTest, MapBSON) { + std::map<std::string, BSONObj> mapBSON = {{"key1", + BSON("str1" + << "str2")}, + {"key2", + BSON("str3" + << "str4")}}; + logd("{}", mapBSON); // NOLINT + validate([&mapBSON](const BSONObj& obj) { + ASSERT_EQUALS(obj.getField(kMessageFieldName).String(), + "(key1: {\"str1\":\"str2\"}, key2: {\"str3\":\"str4\"})"); + }); +} + } // namespace } // namespace mongo::logv2 |