backend.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "backend.h"
  2. #include <library/cpp/logger/backend.h>
  3. #include <library/cpp/logger/record.h>
  4. #include <library/cpp/yt/assert/assert.h>
  5. #include <library/cpp/yt/logging/logger.h>
  6. namespace NYT::NLogging {
  7. namespace {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. ELogLevel ConvertToLogLevel(ELogPriority priority)
  10. {
  11. switch (priority) {
  12. case ELogPriority::TLOG_DEBUG:
  13. return ELogLevel::Debug;
  14. case ELogPriority::TLOG_INFO:
  15. [[fallthrough]];
  16. case ELogPriority::TLOG_NOTICE:
  17. return ELogLevel::Info;
  18. case ELogPriority::TLOG_WARNING:
  19. return ELogLevel::Warning;
  20. case ELogPriority::TLOG_ERR:
  21. return ELogLevel::Error;
  22. case ELogPriority::TLOG_CRIT:
  23. case ELogPriority::TLOG_ALERT:
  24. return ELogLevel::Alert;
  25. case ELogPriority::TLOG_EMERG:
  26. return ELogLevel::Fatal;
  27. case ELogPriority::TLOG_RESOURCES:
  28. return ELogLevel::Maximum;
  29. }
  30. YT_ABORT();
  31. }
  32. class TLogBackendBridge
  33. : public TLogBackend
  34. {
  35. public:
  36. TLogBackendBridge(const TLogger& logger)
  37. : Logger_(logger)
  38. { }
  39. void WriteData(const TLogRecord& rec) override
  40. {
  41. const auto logLevel = ConvertToLogLevel(rec.Priority);
  42. if (!Logger_.IsLevelEnabled(logLevel)) {
  43. return;
  44. }
  45. // Remove trailing \n, because it will add it.
  46. TStringBuf message(rec.Data, rec.Len);
  47. message.ChopSuffix(TStringBuf("\n"));
  48. // Use low-level api, because it is more convinient here.
  49. auto loggingContext = GetLoggingContext();
  50. auto event = NDetail::CreateLogEvent(loggingContext, Logger_, logLevel);
  51. event.MessageRef = NDetail::BuildLogMessage(loggingContext, Logger_, message).MessageRef;
  52. event.Family = ELogFamily::PlainText;
  53. Logger_.Write(std::move(event));
  54. }
  55. void ReopenLog() override
  56. { }
  57. ELogPriority FiltrationLevel() const override
  58. {
  59. return LOG_MAX_PRIORITY;
  60. }
  61. private:
  62. const TLogger Logger_;
  63. };
  64. } // namespace
  65. THolder<TLogBackend> CreateArcadiaLogBackend(const TLogger& logger)
  66. {
  67. return MakeHolder<TLogBackendBridge>(logger);
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. } // namespace NYT::NLogging