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());
57 std::vector<std::string>& getMessages() {
60 const std::vector<std::string>& getMessages() const {
65 std::vector<std::string> messages_;
69 TEST(StandardLogHandler, simple) {
70 auto writer = make_shared<TestLogWriter>();
71 StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
73 LoggerDB db{LoggerDB::TESTING};
74 auto logCategory = db.getCategory("log_cat");
75 auto handlerCategory = db.getCategory("handler_cat");
77 LogMessage msg{logCategory,
81 std::string{"hello world"}};
82 handler.handleMessage(msg, handlerCategory);
83 ASSERT_EQ(1, writer->getMessages().size());
85 "LogLevel::DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
86 writer->getMessages()[0]);
89 TEST(StandardLogHandler, levelCheck) {
90 auto writer = make_shared<TestLogWriter>();
91 StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
93 LoggerDB db{LoggerDB::TESTING};
94 auto logCategory = db.getCategory("log_cat");
95 auto handlerCategory = db.getCategory("handler_cat");
97 auto logMsg = [&](LogLevel level, folly::StringPiece message) {
98 LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
99 handler.handleMessage(msg, handlerCategory);
102 handler.setLevel(LogLevel::WARN);
103 logMsg(LogLevel::INFO, "info");
104 logMsg(LogLevel::WARN, "beware");
105 logMsg(LogLevel::ERR, "whoops");
106 logMsg(LogLevel::DBG1, "debug stuff");
108 auto& messages = writer->getMessages();
109 ASSERT_EQ(2, messages.size());
111 "LogLevel::WARN::log_cat::handler_cat::src/test.cpp::1234::beware",
114 "LogLevel::ERR::log_cat::handler_cat::src/test.cpp::1234::whoops",
118 handler.setLevel(LogLevel::DBG2);
119 logMsg(LogLevel::DBG3, "dbg");
120 logMsg(LogLevel::DBG1, "here");
121 logMsg(LogLevel::DBG2, "and here");
122 logMsg(LogLevel::ERR, "oh noes");
123 logMsg(LogLevel::DBG9, "very verbose");
125 ASSERT_EQ(3, messages.size());
127 "LogLevel::DBG1::log_cat::handler_cat::src/test.cpp::1234::here",
130 "LogLevel::DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
133 "LogLevel::ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes",