2 * Copyright 2004-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/Conv.h>
17 #include <folly/experimental/logging/LogCategory.h>
18 #include <folly/experimental/logging/LogFormatter.h>
19 #include <folly/experimental/logging/LogLevel.h>
20 #include <folly/experimental/logging/LogMessage.h>
21 #include <folly/experimental/logging/LogWriter.h>
22 #include <folly/experimental/logging/LoggerDB.h>
23 #include <folly/experimental/logging/StandardLogHandler.h>
24 #include <folly/portability/GTest.h>
26 using namespace folly;
27 using std::make_shared;
30 class TestLogFormatter : public LogFormatter {
32 std::string formatMessage(
33 const LogMessage& message,
34 const LogCategory* handlerCategory) override {
35 return folly::to<std::string>(
36 logLevelToString(message.getLevel()),
38 message.getCategory()->getName(),
40 handlerCategory->getName(),
42 message.getFileName(),
44 message.getLineNumber(),
46 message.getMessage());
50 class TestLogWriter : public LogWriter {
52 void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
54 messages_.emplace_back(buffer.str());
56 void flush() override {}
58 std::vector<std::string>& getMessages() {
61 const std::vector<std::string>& getMessages() const {
66 std::vector<std::string> messages_;
70 TEST(StandardLogHandler, simple) {
71 auto writer = make_shared<TestLogWriter>();
72 StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
74 LoggerDB db{LoggerDB::TESTING};
75 auto logCategory = db.getCategory("log_cat");
76 auto handlerCategory = db.getCategory("handler_cat");
78 LogMessage msg{logCategory,
82 std::string{"hello world"}};
83 handler.handleMessage(msg, handlerCategory);
84 ASSERT_EQ(1, writer->getMessages().size());
86 "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
87 writer->getMessages()[0]);
90 TEST(StandardLogHandler, levelCheck) {
91 auto writer = make_shared<TestLogWriter>();
92 StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
94 LoggerDB db{LoggerDB::TESTING};
95 auto logCategory = db.getCategory("log_cat");
96 auto handlerCategory = db.getCategory("handler_cat");
98 auto logMsg = [&](LogLevel level, folly::StringPiece message) {
99 LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
100 handler.handleMessage(msg, handlerCategory);
103 handler.setLevel(LogLevel::WARN);
104 logMsg(LogLevel::INFO, "info");
105 logMsg(LogLevel::WARN, "beware");
106 logMsg(LogLevel::ERR, "whoops");
107 logMsg(LogLevel::DBG1, "debug stuff");
109 auto& messages = writer->getMessages();
110 ASSERT_EQ(2, messages.size());
112 "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
114 "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
117 handler.setLevel(LogLevel::DBG2);
118 logMsg(LogLevel::DBG3, "dbg");
119 logMsg(LogLevel::DBG1, "here");
120 logMsg(LogLevel::DBG2, "and here");
121 logMsg(LogLevel::ERR, "oh noes");
122 logMsg(LogLevel::DBG9, "very verbose");
124 ASSERT_EQ(3, messages.size());
126 "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
128 "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
131 "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));