2 * Copyright 2017-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/experimental/logging/StandardLogHandler.h>
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>
28 using namespace folly;
29 using std::make_shared;
32 class TestLogFormatter : public LogFormatter {
34 std::string formatMessage(
35 const LogMessage& message,
36 const LogCategory* handlerCategory) override {
37 return folly::to<std::string>(
38 logLevelToString(message.getLevel()),
40 message.getCategory()->getName(),
42 handlerCategory->getName(),
44 message.getFileName(),
46 message.getLineNumber(),
48 message.getMessage());
52 class TestLogWriter : public LogWriter {
54 void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
56 messages_.emplace_back(buffer.str());
58 void flush() override {}
60 std::vector<std::string>& getMessages() {
63 const std::vector<std::string>& getMessages() const {
68 std::vector<std::string> messages_;
72 TEST(StandardLogHandler, simple) {
73 auto writer = make_shared<TestLogWriter>();
74 LogHandlerConfig config{"std_test"};
75 StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
77 LoggerDB db{LoggerDB::TESTING};
78 auto logCategory = db.getCategory("log_cat");
79 auto handlerCategory = db.getCategory("handler_cat");
81 LogMessage msg{logCategory,
85 std::string{"hello world"}};
86 handler.handleMessage(msg, handlerCategory);
87 ASSERT_EQ(1, writer->getMessages().size());
89 "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
90 writer->getMessages()[0]);
93 TEST(StandardLogHandler, levelCheck) {
94 auto writer = make_shared<TestLogWriter>();
95 LogHandlerConfig config{"std_test"};
96 StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
98 LoggerDB db{LoggerDB::TESTING};
99 auto logCategory = db.getCategory("log_cat");
100 auto handlerCategory = db.getCategory("handler_cat");
102 auto logMsg = [&](LogLevel level, folly::StringPiece message) {
103 LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
104 handler.handleMessage(msg, handlerCategory);
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");
113 auto& messages = writer->getMessages();
114 ASSERT_EQ(2, messages.size());
116 "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
118 "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
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");
128 ASSERT_EQ(3, messages.size());
130 "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
132 "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
135 "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));