From acf62d487edac640b7167e6d8af15246e87e4f15 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 19 Mar 2019 09:32:02 +0100 Subject: [PATCH] Improve logging of GelfWriter --- lib/perfdata/gelfwriter.cpp | 21 +++++++++++---------- lib/perfdata/gelfwriter.hpp | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index d028c5dbd..59fc7dcc4 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -241,10 +241,10 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con fields->Set("_reachable", checkable->IsReachable()); - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); + CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); - if (commandObj) - fields->Set("_check_command", commandObj->GetName()); + if (checkCommand) + fields->Set("_check_command", checkCommand->GetName()); double ts = Utility::GetTime(); @@ -272,8 +272,9 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "GelfWriter") - << "Ignoring invalid perfdata value: '" << val << "' for object '" - << checkable->GetName() << "'."; + << "Ignoring invalid perfdata for checkable '" + << checkable->GetName() << "' and command '" + << checkCommand->GetName() << "' with value: " << val; continue; } } @@ -301,7 +302,7 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con } } - SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts)); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); } void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, @@ -370,7 +371,7 @@ void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& noti if (commandObj) fields->Set("_check_command", commandObj->GetName()); - SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts)); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); } void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) @@ -426,7 +427,7 @@ void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, con ts = cr->GetExecutionEnd(); } - SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts)); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); } String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts) @@ -438,7 +439,7 @@ String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const Strin return JsonEncode(fields); } -void GelfWriter::SendLogMessage(const String& gelfMessage) +void GelfWriter::SendLogMessage(const Checkable::Ptr& checkable, const String& gelfMessage) { std::ostringstream msgbuf; msgbuf << gelfMessage; @@ -453,7 +454,7 @@ void GelfWriter::SendLogMessage(const String& gelfMessage) try { Log(LogDebug, "GelfWriter") - << "Sending '" << log << "'."; + << "Checkable '" << checkable->GetName() << "' sending message '" << log << "'."; m_Stream->Write(log.CStr(), log.GetLength()); } catch (const std::exception& ex) { diff --git a/lib/perfdata/gelfwriter.hpp b/lib/perfdata/gelfwriter.hpp index 167f523d5..6e222d214 100644 --- a/lib/perfdata/gelfwriter.hpp +++ b/lib/perfdata/gelfwriter.hpp @@ -50,7 +50,7 @@ private: void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type); String ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts); - void SendLogMessage(const String& gelfMessage); + void SendLogMessage(const Checkable::Ptr& checkable, const String& gelfMessage); void ReconnectTimerHandler(); -- 2.40.0