robot-ydb-importer 1 год назад
Родитель
Сommit
5b05a95038
2 измененных файлов с 48 добавлено и 19 удалено
  1. 27 12
      yt/yt/core/logging/formatter.cpp
  2. 21 7
      yt/yt/core/logging/formatter.h

+ 27 - 12
yt/yt/core/logging/formatter.cpp

@@ -1,7 +1,6 @@
 #include "formatter.h"
 
 #include "private.h"
-// #include "log.h"
 
 #include <yt/yt/build/build.h>
 
@@ -9,8 +8,6 @@
 
 #include <yt/yt/core/ytree/fluent.h>
 
-// #include <yt/yt/core/yson/writer.h>
-
 #include <util/stream/length.h>
 
 namespace NYT::NLogging {
@@ -92,7 +89,7 @@ TLogEvent GetSkippedLogStructuredEvent(i64 count, TStringBuf skippedBy)
 TPlainTextLogFormatter::TPlainTextLogFormatter(
     bool enableSystemMessages,
     bool enableSourceLocation)
-    : EnableSystemMessages_(enableSystemMessages && Logger)
+    : TLogFormatterBase(enableSystemMessages, enableSourceLocation)
     , EventFormatter_(enableSourceLocation)
 { }
 
@@ -118,20 +115,39 @@ void TPlainTextLogFormatter::WriteLogReopenSeparator(IOutputStream* outputStream
 
 void TPlainTextLogFormatter::WriteLogStartEvent(IOutputStream* outputStream)
 {
-    if (EnableSystemMessages_) {
+    if (AreSystemMessagesEnabled()) {
         WriteFormatted(outputStream, GetStartLogEvent());
     }
 }
 
 void TPlainTextLogFormatter::WriteLogSkippedEvent(IOutputStream* outputStream, i64 count, TStringBuf skippedBy)
 {
-    if (EnableSystemMessages_) {
+    if (AreSystemMessagesEnabled()) {
         WriteFormatted(outputStream, GetSkippedLogEvent(count, skippedBy));
     }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
+TLogFormatterBase::TLogFormatterBase(
+    bool enableSystemMessages,
+    bool enableSourceLocation)
+    : EnableSystemMessages_(enableSystemMessages)
+    , EnableSourceLocation_(enableSourceLocation)
+{ }
+
+bool TLogFormatterBase::AreSystemMessagesEnabled() const
+{
+    return EnableSystemMessages_ && GetDefaultLogManager();
+}
+
+bool TLogFormatterBase::IsSourceLocationEnabled() const
+{
+    return EnableSourceLocation_;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
 TStructuredLogFormatter::TStructuredLogFormatter(
     ELogFormat format,
     THashMap<TString, NYTree::INodePtr> commonFields,
@@ -139,10 +155,9 @@ TStructuredLogFormatter::TStructuredLogFormatter(
     bool enableSourceLocation,
     bool enableInstant,
     NJson::TJsonFormatConfigPtr jsonFormat)
-    : Format_(format)
+    : TLogFormatterBase(enableSystemMessages, enableSourceLocation)
+    , Format_(format)
     , CommonFields_(std::move(commonFields))
-    , EnableSystemMessages_(enableSystemMessages)
-    , EnableSourceLocation_(enableSourceLocation)
     , EnableInstant_(enableInstant)
     , JsonFormat_(!jsonFormat && (Format_ == ELogFormat::Json)
         ? New<NJson::TJsonFormatConfig>()
@@ -196,7 +211,7 @@ i64 TStructuredLogFormatter::WriteFormatted(IOutputStream* stream, const TLogEve
                 if (event.TraceId != TTraceId()) {
                     fluent.Item("trace_id").Value(event.TraceId);
                 }
-                if (EnableSourceLocation_ && event.SourceFile) {
+                if (IsSourceLocationEnabled() && event.SourceFile) {
                     auto sourceFile = event.SourceFile;
                     fluent.Item("source_file").Value(Format("%v:%v", sourceFile.RNextTok(LOCSLASH_C), event.SourceLine));
                 }
@@ -219,14 +234,14 @@ void TStructuredLogFormatter::WriteLogReopenSeparator(IOutputStream* /*outputStr
 
 void TStructuredLogFormatter::WriteLogStartEvent(IOutputStream* outputStream)
 {
-    if (EnableSystemMessages_) {
+    if (AreSystemMessagesEnabled()) {
         WriteFormatted(outputStream, GetStartLogStructuredEvent());
     }
 }
 
 void TStructuredLogFormatter::WriteLogSkippedEvent(IOutputStream* outputStream, i64 count, TStringBuf skippedBy)
 {
-    if (EnableSystemMessages_) {
+    if (AreSystemMessagesEnabled()) {
         WriteFormatted(outputStream, GetSkippedLogStructuredEvent(count, skippedBy));
     }
 }

+ 21 - 7
yt/yt/core/logging/formatter.h

@@ -22,9 +22,27 @@ struct ILogFormatter
 
 ////////////////////////////////////////////////////////////////////////////////
 
-class TPlainTextLogFormatter
+class TLogFormatterBase
     : public ILogFormatter
 {
+protected:
+    TLogFormatterBase(
+        bool enableSystemMessages,
+        bool enableSourceLocation);
+
+    bool AreSystemMessagesEnabled() const;
+    bool IsSourceLocationEnabled() const;
+
+private:
+    const bool EnableSystemMessages_;
+    const bool EnableSourceLocation_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+class TPlainTextLogFormatter
+    : public TLogFormatterBase
+{
 public:
     explicit TPlainTextLogFormatter(
         bool enableControlMessages = true,
@@ -36,8 +54,6 @@ public:
     void WriteLogSkippedEvent(IOutputStream* outputStream, i64 count, TStringBuf skippedBy) override;
 
 private:
-    const bool EnableSystemMessages_;
-
     TRawFormatter<MessageBufferSize> Buffer_;
     TPlainTextEventFormatter EventFormatter_;
 };
@@ -45,13 +61,13 @@ private:
 ////////////////////////////////////////////////////////////////////////////////
 
 class TStructuredLogFormatter
-    : public ILogFormatter
+    : public TLogFormatterBase
 {
 public:
     TStructuredLogFormatter(
         ELogFormat format,
         THashMap<TString, NYTree::INodePtr> commonFields,
-        bool enableControlMessages = true,
+        bool enablSystemlMessages = true,
         bool enableSourceLocation = false,
         bool enableInstant = true,
         NJson::TJsonFormatConfigPtr jsonFormat = nullptr);
@@ -64,8 +80,6 @@ public:
 private:
     const ELogFormat Format_;
     const THashMap<TString, NYTree::INodePtr> CommonFields_;
-    const bool EnableSystemMessages_;
-    const bool EnableSourceLocation_;
     const bool EnableInstant_;
     const NJson::TJsonFormatConfigPtr JsonFormat_;