Fix issues

This commit is contained in:
JCW
2025-08-28 23:04:56 +01:00
parent b5c4fd4c51
commit 52becffa48
2 changed files with 41 additions and 28 deletions

View File

@@ -584,6 +584,13 @@ public:
} }
/** @} */ /** @} */
static void
resetGlobalAttributes()
{
std::lock_guard lock(globalLogAttributesMutex_);
globalLogAttributes_ = std::nullopt;
}
static void static void
addGlobalAttributes(JsonLogAttributes globalLogAttributes) addGlobalAttributes(JsonLogAttributes globalLogAttributes)
{ {

View File

@@ -203,34 +203,6 @@ Journal::formatLog(
rapidjson::Value logContext; rapidjson::Value logContext;
logContext.SetObject(); logContext.SetObject();
if (globalLogAttributes_)
{
for (auto const& [key, value] :
globalLogAttributes_->contextValues().GetObject())
{
rapidjson::Value jsonValue;
jsonValue.CopyFrom(value, currentJsonLogContext_.allocator);
logContext.AddMember(
rapidjson::Value{key, currentJsonLogContext_.allocator},
std::move(jsonValue),
currentJsonLogContext_.allocator);
}
}
if (attributes.has_value())
{
for (auto const& [key, value] : attributes->contextValues().GetObject())
{
rapidjson::Value jsonValue;
jsonValue.CopyFrom(value, currentJsonLogContext_.allocator);
logContext.AddMember(
rapidjson::Value{key, currentJsonLogContext_.allocator},
std::move(jsonValue),
currentJsonLogContext_.allocator);
}
}
logContext.AddMember( logContext.AddMember(
rapidjson::StringRef("Function"), rapidjson::StringRef("Function"),
rapidjson::StringRef(currentJsonLogContext_.location.function_name()), rapidjson::StringRef(currentJsonLogContext_.location.function_name()),
@@ -274,6 +246,39 @@ Journal::formatLog(
.count(), .count(),
currentJsonLogContext_.allocator); currentJsonLogContext_.allocator);
if (attributes.has_value())
{
for (auto const& [key, value] : attributes->contextValues().GetObject())
{
if (logContext.HasMember(key))
continue;
rapidjson::Value jsonValue;
jsonValue.CopyFrom(value, currentJsonLogContext_.allocator);
logContext.AddMember(
rapidjson::Value{key, currentJsonLogContext_.allocator},
std::move(jsonValue),
currentJsonLogContext_.allocator);
}
}
if (globalLogAttributes_)
{
for (auto const& [key, value] :
globalLogAttributes_->contextValues().GetObject())
{
if (logContext.HasMember(key))
continue;
rapidjson::Value jsonValue;
jsonValue.CopyFrom(value, currentJsonLogContext_.allocator);
logContext.AddMember(
rapidjson::Value{key, currentJsonLogContext_.allocator},
std::move(jsonValue),
currentJsonLogContext_.allocator);
}
}
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer writer(buffer); rapidjson::Writer writer(buffer);
@@ -292,6 +297,7 @@ void
Journal::disableStructuredJournal() Journal::disableStructuredJournal()
{ {
m_jsonLogsEnabled = false; m_jsonLogsEnabled = false;
resetGlobalAttributes();
} }
bool bool