composite_ut.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "log.h"
  2. #include <library/cpp/logger/init_context/config.h>
  3. #include <library/cpp/logger/init_context/yconf.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <library/cpp/yconf/patcher/unstrict_config.h>
  6. #include <util/stream/file.h>
  7. #include <util/system/fs.h>
  8. Y_UNIT_TEST_SUITE(TCompositeLogTest)
  9. {
  10. TVector<TString> ReadLines(const TString & filename) {
  11. TVector<TString> lines;
  12. TIFStream fin(filename);
  13. TString line;
  14. while (fin.ReadLine(line)) {
  15. lines.push_back(std::move(line));
  16. }
  17. return lines;
  18. }
  19. void Clear(const TString & filename) {
  20. NFs::Remove(filename + "1");
  21. NFs::Remove(filename + "2");
  22. }
  23. void DoTestComposite(const ILogBackendCreator::IInitContext& ctx, const TString & filename) {
  24. Clear(filename);
  25. {
  26. TLog log;
  27. {
  28. auto creator = ILogBackendCreator::Create(ctx);
  29. log.ResetBackend(creator->CreateLogBackend());
  30. log.ReopenLog();
  31. }
  32. log.Write(TLOG_ERR, "first\n");
  33. log.Write(TLOG_DEBUG, "second\n");
  34. }
  35. auto data1 = ReadLines(filename + "1");
  36. auto data2 = ReadLines(filename + "2");
  37. UNIT_ASSERT_VALUES_EQUAL(data1.size(), 2);
  38. UNIT_ASSERT(data1[0] == "first");
  39. UNIT_ASSERT(data1[1] == "second");
  40. UNIT_ASSERT_VALUES_EQUAL(data2.size(), 1);
  41. UNIT_ASSERT(data2[0] == "first");
  42. Clear(filename);
  43. }
  44. Y_UNIT_TEST(TestCompositeConfig) {
  45. TString s(R"(
  46. {
  47. "LoggerType": "composite",
  48. "SubLogger":[
  49. {
  50. "LoggerType": "file",
  51. "Path": "config_log_1"
  52. }, {
  53. "LoggerType": "config_log_2",
  54. "LogLevel": "INFO"
  55. }
  56. ]
  57. })");
  58. TStringInput si(s);
  59. NConfig::TConfig cfg = NConfig::TConfig::FromJson(si);
  60. //Прогоняем конфигурацию через серализацию и десериализацию
  61. TLogBackendCreatorInitContextConfig ctx(cfg);
  62. TString newCfg = ILogBackendCreator::Create(ctx)->AsJson().GetStringRobust();
  63. TStringInput si2(newCfg);
  64. DoTestComposite(TLogBackendCreatorInitContextConfig(NConfig::TConfig::FromJson(si2)), "config_log_");
  65. }
  66. Y_UNIT_TEST(TestCompositeYConf) {
  67. constexpr const char* CONFIG = R"(
  68. <Logger>
  69. LoggerType: composite
  70. <SubLogger>
  71. LoggerType: file
  72. Path: yconf_log_1
  73. </SubLogger>
  74. <SubLogger>
  75. LoggerType: yconf_log_2
  76. LogLevel: INFO
  77. </SubLogger>
  78. </Logger>
  79. )";
  80. TUnstrictConfig cfg;
  81. if (!cfg.ParseMemory(CONFIG)) {
  82. TString errors;
  83. cfg.PrintErrors(errors);
  84. UNIT_ASSERT_C(false, errors);
  85. }
  86. TLogBackendCreatorInitContextYConf ctx(*cfg.GetFirstChild("Logger"));
  87. //Прогоняем конфигурацию через серализацию и десериализацию
  88. TUnstrictConfig newCfg;
  89. UNIT_ASSERT(newCfg.ParseJson(ILogBackendCreator::Create(ctx)->AsJson()));
  90. DoTestComposite(TLogBackendCreatorInitContextYConf(*newCfg.GetRootSection()), "yconf_log_");
  91. }
  92. }