logging: add a LogHandler::getConfig() method
[folly.git] / folly / experimental / logging / test / StandardLogHandlerTest.cpp
1 /*
2  * Copyright 2004-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/experimental/logging/StandardLogHandler.h>
17
18 #include <folly/Conv.h>
19 #include <folly/experimental/logging/LogCategory.h>
20 #include <folly/experimental/logging/LogFormatter.h>
21 #include <folly/experimental/logging/LogHandlerConfig.h>
22 #include <folly/experimental/logging/LogLevel.h>
23 #include <folly/experimental/logging/LogMessage.h>
24 #include <folly/experimental/logging/LogWriter.h>
25 #include <folly/experimental/logging/LoggerDB.h>
26 #include <folly/portability/GTest.h>
27
28 using namespace folly;
29 using std::make_shared;
30
31 namespace {
32 class TestLogFormatter : public LogFormatter {
33  public:
34   std::string formatMessage(
35       const LogMessage& message,
36       const LogCategory* handlerCategory) override {
37     return folly::to<std::string>(
38         logLevelToString(message.getLevel()),
39         "::",
40         message.getCategory()->getName(),
41         "::",
42         handlerCategory->getName(),
43         "::",
44         message.getFileName(),
45         "::",
46         message.getLineNumber(),
47         "::",
48         message.getMessage());
49   }
50 };
51
52 class TestLogWriter : public LogWriter {
53  public:
54   void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
55       override {
56     messages_.emplace_back(buffer.str());
57   }
58   void flush() override {}
59
60   std::vector<std::string>& getMessages() {
61     return messages_;
62   }
63   const std::vector<std::string>& getMessages() const {
64     return messages_;
65   }
66
67  private:
68   std::vector<std::string> messages_;
69 };
70 } // namespace
71
72 TEST(StandardLogHandler, simple) {
73   auto writer = make_shared<TestLogWriter>();
74   LogHandlerConfig config{"std_test"};
75   StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
76
77   LoggerDB db{LoggerDB::TESTING};
78   auto logCategory = db.getCategory("log_cat");
79   auto handlerCategory = db.getCategory("handler_cat");
80
81   LogMessage msg{logCategory,
82                  LogLevel::DBG8,
83                  "src/test.cpp",
84                  1234,
85                  std::string{"hello world"}};
86   handler.handleMessage(msg, handlerCategory);
87   ASSERT_EQ(1, writer->getMessages().size());
88   EXPECT_EQ(
89       "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
90       writer->getMessages()[0]);
91 }
92
93 TEST(StandardLogHandler, levelCheck) {
94   auto writer = make_shared<TestLogWriter>();
95   LogHandlerConfig config{"std_test"};
96   StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
97
98   LoggerDB db{LoggerDB::TESTING};
99   auto logCategory = db.getCategory("log_cat");
100   auto handlerCategory = db.getCategory("handler_cat");
101
102   auto logMsg = [&](LogLevel level, folly::StringPiece message) {
103     LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
104     handler.handleMessage(msg, handlerCategory);
105   };
106
107   handler.setLevel(LogLevel::WARN);
108   logMsg(LogLevel::INFO, "info");
109   logMsg(LogLevel::WARN, "beware");
110   logMsg(LogLevel::ERR, "whoops");
111   logMsg(LogLevel::DBG1, "debug stuff");
112
113   auto& messages = writer->getMessages();
114   ASSERT_EQ(2, messages.size());
115   EXPECT_EQ(
116       "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
117   EXPECT_EQ(
118       "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
119   messages.clear();
120
121   handler.setLevel(LogLevel::DBG2);
122   logMsg(LogLevel::DBG3, "dbg");
123   logMsg(LogLevel::DBG1, "here");
124   logMsg(LogLevel::DBG2, "and here");
125   logMsg(LogLevel::ERR, "oh noes");
126   logMsg(LogLevel::DBG9, "very verbose");
127
128   ASSERT_EQ(3, messages.size());
129   EXPECT_EQ(
130       "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
131   EXPECT_EQ(
132       "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
133       messages.at(1));
134   EXPECT_EQ(
135       "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));
136   messages.clear();
137 }