aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornadya73 <nadya73@yandex-team.com>2023-09-14 13:05:39 +0300
committernadya73 <nadya73@yandex-team.com>2023-09-14 13:35:57 +0300
commitd32763a27b11f69d4d6e379d966847174fa19054 (patch)
tree5e3907ee85f704d6c0bec17c8cffa8620d6582a5
parentca564a79a442b31fbc7689b4d4c21250e7d2d53e (diff)
downloadydb-d32763a27b11f69d4d6e379d966847174fa19054.tar.gz
Write fiberId, traceId and sourceFile for usual logs in yson/json
-rw-r--r--yt/yt/core/logging/formatter.cpp14
-rw-r--r--yt/yt/core/logging/formatter.h2
-rw-r--r--yt/yt/core/logging/log_manager.cpp1
-rw-r--r--yt/yt/core/logging/unittests/logging_ut.cpp55
4 files changed, 72 insertions, 0 deletions
diff --git a/yt/yt/core/logging/formatter.cpp b/yt/yt/core/logging/formatter.cpp
index a8bf825af5..701f8a87c5 100644
--- a/yt/yt/core/logging/formatter.cpp
+++ b/yt/yt/core/logging/formatter.cpp
@@ -136,10 +136,12 @@ TStructuredLogFormatter::TStructuredLogFormatter(
ELogFormat format,
THashMap<TString, NYTree::INodePtr> commonFields,
bool enableSystemMessages,
+ bool enableSourceLocation,
NJson::TJsonFormatConfigPtr jsonFormat)
: Format_(format)
, CommonFields_(std::move(commonFields))
, EnableSystemMessages_(enableSystemMessages)
+ , EnableSourceLocation_(enableSourceLocation)
, JsonFormat_(!jsonFormat && (Format_ == ELogFormat::Json)
? New<NJson::TJsonFormatConfig>()
: std::move(jsonFormat))
@@ -183,6 +185,18 @@ i64 TStructuredLogFormatter::WriteFormatted(IOutputStream* stream, const TLogEve
.Item("instant").Value(dateTimeBuffer.GetBuffer())
.Item("level").Value(FormatEnum(event.Level))
.Item("category").Value(event.Category->Name)
+ .DoIf(event.Family == ELogFamily::PlainText, [&] (auto fluent) {
+ if (event.FiberId != TFiberId()) {
+ fluent.Item("fiberId").Value(Format("%x", event.FiberId));
+ }
+ if (event.TraceId != TTraceId()) {
+ fluent.Item("traceId").Value(event.TraceId);
+ }
+ if (EnableSourceLocation_ && event.SourceFile) {
+ auto sourceFile = event.SourceFile;
+ fluent.Item("sourceFile").Value(Format("%v:%v", sourceFile.RNextTok(LOCSLASH_C), event.SourceLine));
+ }
+ })
.EndMap();
consumer->Flush();
diff --git a/yt/yt/core/logging/formatter.h b/yt/yt/core/logging/formatter.h
index 1be9994aa6..4d1086d51d 100644
--- a/yt/yt/core/logging/formatter.h
+++ b/yt/yt/core/logging/formatter.h
@@ -52,6 +52,7 @@ public:
ELogFormat format,
THashMap<TString, NYTree::INodePtr> commonFields,
bool enableControlMessages = true,
+ bool enableSourceLocation = false,
NJson::TJsonFormatConfigPtr jsonFormat = nullptr);
i64 WriteFormatted(IOutputStream* outputStream, const TLogEvent& event) override;
@@ -63,6 +64,7 @@ private:
const ELogFormat Format_;
const THashMap<TString, NYTree::INodePtr> CommonFields_;
const bool EnableSystemMessages_;
+ const bool EnableSourceLocation_;
const NJson::TJsonFormatConfigPtr JsonFormat_;
TCachingDateFormatter CachingDateFormatter_;
diff --git a/yt/yt/core/logging/log_manager.cpp b/yt/yt/core/logging/log_manager.cpp
index 976fe387bf..5b509b9470 100644
--- a/yt/yt/core/logging/log_manager.cpp
+++ b/yt/yt/core/logging/log_manager.cpp
@@ -844,6 +844,7 @@ private:
writerConfig->Format,
writerConfig->CommonFields,
writerConfig->AreSystemMessagesEnabled(),
+ writerConfig->EnableSourceLocation,
writerConfig->JsonFormat);
default:
diff --git a/yt/yt/core/logging/unittests/logging_ut.cpp b/yt/yt/core/logging/unittests/logging_ut.cpp
index 5f2d5c7b27..20e1eb128e 100644
--- a/yt/yt/core/logging/unittests/logging_ut.cpp
+++ b/yt/yt/core/logging/unittests/logging_ut.cpp
@@ -503,6 +503,54 @@ TEST_F(TLoggingTest, ThreadMinLogLevel)
}
}
+TEST_F(TLoggingTest, PlainTextLoggingStructuredFormatter)
+{
+ TLogEvent event;
+ event.Family = ELogFamily::PlainText;
+ event.Category = Logger.GetCategory();
+ event.Level = ELogLevel::Debug;
+ event.MessageRef = TSharedRef::FromString("test_message");
+ event.MessageKind = ELogMessageKind::Unstructured;
+ event.FiberId = 31;
+ event.TraceId = TGuid(1, 2, 3, 4);
+ event.SourceFile = "a/b.cpp";
+ event.SourceLine = 123;
+
+ for (auto enableSourceLocation : {false, true}) {
+ for (auto format : {ELogFormat::Yson, ELogFormat::Json}) {
+ TTempFile logFile(GenerateLogFileName());
+
+ auto writerConfig = New<TFileLogWriterConfig>();
+ writerConfig->FileName = logFile.Name();
+
+ auto writer = CreateFileLogWriter(
+ std::make_unique<TStructuredLogFormatter>(format, THashMap<TString, INodePtr>{}, /*enableControllMessages*/ true, enableSourceLocation),
+ "test_writer",
+ writerConfig,
+ this);
+
+ WriteEvent(writer, event);
+ TLogManager::Get()->Synchronize();
+
+ auto lines = ReadPlainTextEvents(logFile.Name());
+ EXPECT_EQ(1, std::ssize(lines));
+
+ auto message = DeserializeStructuredEvent(lines[0], format);
+ EXPECT_EQ(message->GetChildOrThrow("message")->AsString()->GetValue(), "test_message");
+ EXPECT_EQ(message->GetChildOrThrow("level")->AsString()->GetValue(), "debug");
+ EXPECT_EQ(message->GetChildOrThrow("category")->AsString()->GetValue(), Logger.GetCategory()->Name);
+ EXPECT_EQ(message->GetChildOrThrow("fiberId")->AsString()->GetValue(), "1f");
+ EXPECT_EQ(message->GetChildOrThrow("traceId")->AsString()->GetValue(), "4-3-2-1");
+
+ if (enableSourceLocation) {
+ EXPECT_EQ(message->GetChildOrThrow("sourceFile")->AsString()->GetValue(), "b.cpp:123");
+ } else {
+ EXPECT_EQ(message->FindChild("sourceFile"), nullptr);
+ }
+ }
+ }
+}
+
TEST_F(TLoggingTest, StructuredLogging)
{
TLogEvent event;
@@ -515,6 +563,9 @@ TEST_F(TLoggingTest, StructuredLogging)
.ToSharedRef();
event.MessageKind = ELogMessageKind::Structured;
+ event.FiberId = 31;
+ event.TraceId = TGuid(1, 2, 3, 4);
+
for (auto format : {ELogFormat::Yson, ELogFormat::Json}) {
TTempFile logFile(GenerateLogFileName());
@@ -537,6 +588,9 @@ TEST_F(TLoggingTest, StructuredLogging)
EXPECT_EQ(message->GetChildOrThrow("message")->AsString()->GetValue(), "test_message");
EXPECT_EQ(message->GetChildOrThrow("level")->AsString()->GetValue(), "debug");
EXPECT_EQ(message->GetChildOrThrow("category")->AsString()->GetValue(), Logger.GetCategory()->Name);
+
+ EXPECT_EQ(message->FindChild("fiberId"), nullptr);
+ EXPECT_EQ(message->FindChild("traceId"), nullptr);
}
}
@@ -604,6 +658,7 @@ TEST_F(TLoggingTest, StructuredLoggingJsonFormat)
ELogFormat::Json,
/*commonFields*/ THashMap<TString, INodePtr>{},
/*enableControlMessages*/ true,
+ /*enableSourceLocation*/ false,
jsonFormat);
auto writer = CreateFileLogWriter(