aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorm-milkin <m-milkin@yandex-team.com>2023-04-14 15:40:27 +0300
committerm-milkin <m-milkin@yandex-team.com>2023-04-14 15:40:27 +0300
commitfb642d882910bcf70b25fa17c3de522e61b4ac84 (patch)
tree5eb54fe4209510f211280aaaba63ede17ad1e2ea /library/cpp
parent8c60d32628e9e0c4659bd837a357b08973070031 (diff)
downloadydb-fb642d882910bcf70b25fa17c3de522e61b4ac84.tar.gz
[library/cpp/logger] Support meta flags with TLogElement
Support opportunity to use TMetaFlags with TLogElement. **Use example:** `Log().With("Property", "Value").With("Code", "Code") << LogMessage` Such interface allows to not parse required by TLogBackend properties from raw string.
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/logger/element.cpp3
-rw-r--r--library/cpp/logger/element.h26
-rw-r--r--library/cpp/logger/element_ut.cpp16
-rw-r--r--library/cpp/logger/stream.cpp21
-rw-r--r--library/cpp/logger/stream.h22
5 files changed, 76 insertions, 12 deletions
diff --git a/library/cpp/logger/element.cpp b/library/cpp/logger/element.cpp
index b510fe16e1..ecce365f1f 100644
--- a/library/cpp/logger/element.cpp
+++ b/library/cpp/logger/element.cpp
@@ -3,6 +3,7 @@
#include <utility>
+
TLogElement::TLogElement(const TLog* parent)
: Parent_(parent)
, Priority_(Parent_->DefaultPriority())
@@ -32,7 +33,7 @@ void TLogElement::DoFlush() {
const size_t filled = Filled();
if (filled) {
- Parent_->Write(Priority_, Data(), filled);
+ Parent_->Write(Priority_, Data(), filled, std::move(Context_));
Reset();
}
}
diff --git a/library/cpp/logger/element.h b/library/cpp/logger/element.h
index fc9bff851f..9ba27ab67e 100644
--- a/library/cpp/logger/element.h
+++ b/library/cpp/logger/element.h
@@ -1,18 +1,20 @@
#pragma once
#include "priority.h"
+#include "record.h"
+
+#include <util/string/cast.h>
#include <util/stream/tempbuf.h>
+
class TLog;
-/*
- * better do not use directly
- */
+/// @warning Better don't use directly.
class TLogElement: public TTempBufOutput {
public:
- TLogElement(const TLog* parent);
- TLogElement(const TLog* parent, ELogPriority priority);
+ explicit TLogElement(const TLog* parent);
+ explicit TLogElement(const TLog* parent, ELogPriority priority);
TLogElement(TLogElement&&) noexcept = default;
TLogElement& operator=(TLogElement&&) noexcept = default;
@@ -26,15 +28,20 @@ public:
return *this;
}
- /*
- * for pretty usage: logger << TLOG_ERROR << "Error description";
- */
+ /// @note For pretty usage: logger << TLOG_ERROR << "Error description";
inline TLogElement& operator<<(ELogPriority priority) {
Flush();
Priority_ = priority;
return *this;
}
+ template<typename T>
+ TLogElement& With(const TStringBuf key, const T value) {
+ Context_.emplace_back(key, ToString(value));
+
+ return *this;
+ }
+
ELogPriority Priority() const noexcept {
return Priority_;
}
@@ -43,6 +50,7 @@ protected:
void DoFlush() override;
protected:
- const TLog* Parent_;
+ const TLog* Parent_ = nullptr;
ELogPriority Priority_;
+ TLogRecord::TMetaFlags Context_;
};
diff --git a/library/cpp/logger/element_ut.cpp b/library/cpp/logger/element_ut.cpp
index 32edc52dfb..86303973d8 100644
--- a/library/cpp/logger/element_ut.cpp
+++ b/library/cpp/logger/element_ut.cpp
@@ -9,13 +9,16 @@
#include <library/cpp/testing/unittest/registar.h>
+
class TLogElementTest: public TTestBase {
UNIT_TEST_SUITE(TLogElementTest);
UNIT_TEST(TestMoveCtor);
+ UNIT_TEST(TestWith);
UNIT_TEST_SUITE_END();
public:
void TestMoveCtor();
+ void TestWith();
};
UNIT_TEST_SUITE_REGISTRATION(TLogElementTest);
@@ -37,3 +40,16 @@ void TLogElementTest::TestMoveCtor() {
dst.Destroy();
UNIT_ASSERT(output.Str() == message);
}
+
+void TLogElementTest::TestWith() {
+ TStringStream output;
+ TLog log(MakeHolder<TStreamWithContextLogBackend>(&output));
+
+ THolder<TLogElement> src = MakeHolder<TLogElement>(&log);
+
+ TString message = "Hello, World!";
+ (*src).With("Foo", "Bar").With("Foo", "Baz") << message;
+
+ src.Destroy();
+ UNIT_ASSERT(output.Str() == "Hello, World!; Foo=Bar; Foo=Baz; ");
+}
diff --git a/library/cpp/logger/stream.cpp b/library/cpp/logger/stream.cpp
index 96787ad94b..73cd5030ac 100644
--- a/library/cpp/logger/stream.cpp
+++ b/library/cpp/logger/stream.cpp
@@ -3,6 +3,7 @@
#include <util/stream/output.h>
+
TStreamLogBackend::TStreamLogBackend(IOutputStream* slave)
: Slave_(slave)
{
@@ -17,3 +18,23 @@ void TStreamLogBackend::WriteData(const TLogRecord& rec) {
void TStreamLogBackend::ReopenLog() {
}
+
+TStreamWithContextLogBackend::TStreamWithContextLogBackend(IOutputStream* slave)
+ : Slave_(slave)
+{
+}
+
+TStreamWithContextLogBackend::~TStreamWithContextLogBackend() {
+}
+
+void TStreamWithContextLogBackend::WriteData(const TLogRecord& rec) {
+ Slave_->Write(rec.Data, rec.Len);
+ Slave_->Write(DELIMITER);
+ for (const auto& [key, value] : rec.MetaFlags) {
+ Slave_->Write(TString::Join(key, "=", value));
+ Slave_->Write(DELIMITER);
+ }
+}
+
+void TStreamWithContextLogBackend::ReopenLog() {
+}
diff --git a/library/cpp/logger/stream.h b/library/cpp/logger/stream.h
index feb240afcb..a5eca9060e 100644
--- a/library/cpp/logger/stream.h
+++ b/library/cpp/logger/stream.h
@@ -2,11 +2,14 @@
#include "backend.h"
+#include <util/generic/string.h>
+
+
class IOutputStream;
-class TStreamLogBackend: public TLogBackend {
+class TStreamLogBackend : public TLogBackend {
public:
- TStreamLogBackend(IOutputStream* slave);
+ explicit TStreamLogBackend(IOutputStream* slave);
~TStreamLogBackend() override;
void WriteData(const TLogRecord& rec) override;
@@ -15,3 +18,18 @@ public:
private:
IOutputStream* Slave_;
};
+
+class TStreamWithContextLogBackend : public TLogBackend {
+private:
+ static constexpr TStringBuf DELIMITER = "; ";
+
+public:
+ explicit TStreamWithContextLogBackend(IOutputStream* slave);
+ ~TStreamWithContextLogBackend() override;
+
+ void WriteData(const TLogRecord& rec) override;
+ void ReopenLog() override;
+
+private:
+ IOutputStream* Slave_;
+};