StandardLogHandlerTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/logging/StandardLogHandler.h>
  17. #include <folly/Conv.h>
  18. #include <folly/logging/LogCategory.h>
  19. #include <folly/logging/LogFormatter.h>
  20. #include <folly/logging/LogHandlerConfig.h>
  21. #include <folly/logging/LogLevel.h>
  22. #include <folly/logging/LogMessage.h>
  23. #include <folly/logging/LogWriter.h>
  24. #include <folly/logging/LoggerDB.h>
  25. #include <folly/portability/GTest.h>
  26. using namespace folly;
  27. using std::make_shared;
  28. namespace {
  29. class TestLogFormatter : public LogFormatter {
  30. public:
  31. std::string formatMessage(
  32. const LogMessage& message,
  33. const LogCategory* handlerCategory) override {
  34. return folly::to<std::string>(
  35. logLevelToString(message.getLevel()),
  36. "::",
  37. message.getCategory()->getName(),
  38. "::",
  39. handlerCategory->getName(),
  40. "::",
  41. message.getFileName(),
  42. "::",
  43. message.getLineNumber(),
  44. "::",
  45. message.getMessage());
  46. }
  47. };
  48. class TestLogWriter : public LogWriter {
  49. public:
  50. void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
  51. override {
  52. messages_.emplace_back(buffer.str());
  53. }
  54. void flush() override {}
  55. std::vector<std::string>& getMessages() {
  56. return messages_;
  57. }
  58. const std::vector<std::string>& getMessages() const {
  59. return messages_;
  60. }
  61. bool ttyOutput() const override {
  62. return false;
  63. }
  64. private:
  65. std::vector<std::string> messages_;
  66. };
  67. } // namespace
  68. TEST(StandardLogHandler, simple) {
  69. auto writer = make_shared<TestLogWriter>();
  70. LogHandlerConfig config{"std_test"};
  71. StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
  72. LoggerDB db{LoggerDB::TESTING};
  73. auto logCategory = db.getCategory("log_cat");
  74. auto handlerCategory = db.getCategory("handler_cat");
  75. LogMessage msg{logCategory,
  76. LogLevel::DBG8,
  77. "src/test.cpp",
  78. 1234,
  79. "testMethod",
  80. std::string{"hello world"}};
  81. handler.handleMessage(msg, handlerCategory);
  82. ASSERT_EQ(1, writer->getMessages().size());
  83. EXPECT_EQ(
  84. "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
  85. writer->getMessages()[0]);
  86. }
  87. TEST(StandardLogHandler, levelCheck) {
  88. auto writer = make_shared<TestLogWriter>();
  89. LogHandlerConfig config{"std_test"};
  90. StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
  91. LoggerDB db{LoggerDB::TESTING};
  92. auto logCategory = db.getCategory("log_cat");
  93. auto handlerCategory = db.getCategory("handler_cat");
  94. auto logMsg = [&](LogLevel level, folly::StringPiece message) {
  95. LogMessage msg{
  96. logCategory, level, "src/test.cpp", 1234, "testMethod", message};
  97. handler.handleMessage(msg, handlerCategory);
  98. };
  99. handler.setLevel(LogLevel::WARN);
  100. logMsg(LogLevel::INFO, "info");
  101. logMsg(LogLevel::WARN, "beware");
  102. logMsg(LogLevel::ERR, "whoops");
  103. logMsg(LogLevel::DBG1, "debug stuff");
  104. auto& messages = writer->getMessages();
  105. ASSERT_EQ(2, messages.size());
  106. EXPECT_EQ(
  107. "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
  108. EXPECT_EQ(
  109. "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
  110. messages.clear();
  111. handler.setLevel(LogLevel::DBG2);
  112. logMsg(LogLevel::DBG3, "dbg");
  113. logMsg(LogLevel::DBG1, "here");
  114. logMsg(LogLevel::DBG2, "and here");
  115. logMsg(LogLevel::ERR, "oh noes");
  116. logMsg(LogLevel::DBG9, "very verbose");
  117. ASSERT_EQ(3, messages.size());
  118. EXPECT_EQ(
  119. "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
  120. EXPECT_EQ(
  121. "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
  122. messages.at(1));
  123. EXPECT_EQ(
  124. "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));
  125. messages.clear();
  126. }