Commit 95d99350 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

logging: add a LogHandler::getConfig() method

Summary:
Add a method to LogHandler to return its current configuration.  This will
make it possible to query the LoggerDB for its current configuration state.

Reviewed By: bolinfest

Differential Revision: D6200563

fbshipit-source-id: 2b8b9752bbeb26c8aac28d1a73b7e2312fd198c8
parent ae69959c
......@@ -119,7 +119,8 @@ std::shared_ptr<LogHandler> FileHandlerFactory::createHandler(
writer = make_shared<ImmediateFileWriter>(std::move(outputFile));
}
return make_shared<StandardLogHandler>(formatter, writer);
return make_shared<StandardLogHandler>(
LogHandlerConfig{getType(), options}, formatter, writer);
}
} // namespace folly
......@@ -19,6 +19,7 @@
#include <folly/experimental/logging/GlogStyleFormatter.h>
#include <folly/experimental/logging/ImmediateFileWriter.h>
#include <folly/experimental/logging/LogCategory.h>
#include <folly/experimental/logging/LogHandlerConfig.h>
#include <folly/experimental/logging/LoggerDB.h>
#include <folly/experimental/logging/StandardLogHandler.h>
......@@ -51,13 +52,16 @@ void initLoggingGlogStyle(
// Create the LogHandler
std::shared_ptr<LogWriter> writer;
folly::File file{STDERR_FILENO, false};
LogHandlerConfig handlerConfig{"file", {{"stream", "stderr"}}};
if (asyncWrites) {
writer = std::make_shared<AsyncFileWriter>(std::move(file));
handlerConfig.options.emplace("async", "true");
} else {
writer = std::make_shared<ImmediateFileWriter>(std::move(file));
handlerConfig.options.emplace("async", "false");
}
auto handler = std::make_shared<StandardLogHandler>(
std::make_shared<GlogStyleFormatter>(), std::move(writer));
handlerConfig, std::make_shared<GlogStyleFormatter>(), std::move(writer));
// Add the handler to the root category.
LoggerDB::get()->getCategory(".")->addHandler(std::move(handler));
......
......@@ -22,6 +22,7 @@
namespace folly {
class LogCategory;
class LogHandlerConfig;
class LogMessage;
/**
......@@ -79,5 +80,11 @@ class LogHandler {
* started will not necessarily be processed by the flush call.
*/
virtual void flush() = 0;
/**
* Return a LogHandlerConfig object describing the configuration of this
* LogHandler.
*/
virtual LogHandlerConfig getConfig() const = 0;
};
} // namespace folly
......@@ -22,9 +22,12 @@
namespace folly {
StandardLogHandler::StandardLogHandler(
LogHandlerConfig config,
std::shared_ptr<LogFormatter> formatter,
std::shared_ptr<LogWriter> writer)
: formatter_{std::move(formatter)}, writer_{std::move(writer)} {}
: formatter_{std::move(formatter)},
writer_{std::move(writer)},
config_{config} {}
StandardLogHandler::~StandardLogHandler() {}
......@@ -40,4 +43,9 @@ void StandardLogHandler::handleMessage(
void StandardLogHandler::flush() {
writer_->flush();
}
LogHandlerConfig StandardLogHandler::getConfig() const {
return config_;
}
} // namespace folly
......@@ -20,6 +20,7 @@
#include <folly/File.h>
#include <folly/Range.h>
#include <folly/experimental/logging/LogHandler.h>
#include <folly/experimental/logging/LogHandlerConfig.h>
namespace folly {
......@@ -40,6 +41,7 @@ class LogWriter;
class StandardLogHandler : public LogHandler {
public:
StandardLogHandler(
LogHandlerConfig config,
std::shared_ptr<LogFormatter> formatter,
std::shared_ptr<LogWriter> writer);
~StandardLogHandler();
......@@ -83,16 +85,19 @@ class StandardLogHandler : public LogHandler {
void flush() override;
LogHandlerConfig getConfig() const override;
private:
std::atomic<LogLevel> level_{LogLevel::NONE};
// The formatter_ and writer_ member variables are const, and cannot be
// modified after the StandardLogHandler is constructed. This allows them to
// be accessed without locking when handling a message. To change these
// values, create a new StandardLogHandler object and replace the old handler
// with the new one in the LoggerDB.
// The following variables are const, and cannot be modified after the
// log handler is constructed. This allows us to access them without
// locking when handling a message. To change these values, create a new
// StandardLogHandler object and replace the old handler with the new one in
// the LoggerDB.
const std::shared_ptr<LogFormatter> formatter_;
const std::shared_ptr<LogWriter> writer_;
const LogHandlerConfig config_;
};
} // namespace folly
......@@ -13,14 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/experimental/logging/StandardLogHandler.h>
#include <folly/Conv.h>
#include <folly/experimental/logging/LogCategory.h>
#include <folly/experimental/logging/LogFormatter.h>
#include <folly/experimental/logging/LogHandlerConfig.h>
#include <folly/experimental/logging/LogLevel.h>
#include <folly/experimental/logging/LogMessage.h>
#include <folly/experimental/logging/LogWriter.h>
#include <folly/experimental/logging/LoggerDB.h>
#include <folly/experimental/logging/StandardLogHandler.h>
#include <folly/portability/GTest.h>
using namespace folly;
......@@ -69,7 +71,8 @@ class TestLogWriter : public LogWriter {
TEST(StandardLogHandler, simple) {
auto writer = make_shared<TestLogWriter>();
StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
LogHandlerConfig config{"std_test"};
StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
LoggerDB db{LoggerDB::TESTING};
auto logCategory = db.getCategory("log_cat");
......@@ -89,7 +92,8 @@ TEST(StandardLogHandler, simple) {
TEST(StandardLogHandler, levelCheck) {
auto writer = make_shared<TestLogWriter>();
StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
LogHandlerConfig config{"std_test"};
StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
LoggerDB db{LoggerDB::TESTING};
auto logCategory = db.getCategory("log_cat");
......
......@@ -15,10 +15,13 @@
*/
#pragma once
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <folly/experimental/logging/LogHandler.h>
#include <folly/experimental/logging/LogHandlerConfig.h>
#include <folly/experimental/logging/LogMessage.h>
namespace folly {
......@@ -31,6 +34,10 @@ namespace folly {
*/
class TestLogHandler : public LogHandler {
public:
TestLogHandler() : config_{"test"} {}
explicit TestLogHandler(LogHandlerConfig config)
: config_{std::move(config)} {}
std::vector<std::pair<LogMessage, const LogCategory*>>& getMessages() {
return messages_;
}
......@@ -49,8 +56,14 @@ class TestLogHandler : public LogHandler {
return flushCount_;
}
LogHandlerConfig getConfig() const override {
return config_;
}
private:
std::vector<std::pair<LogMessage, const LogCategory*>> messages_;
uint64_t flushCount_{0};
std::map<std::string, std::string> options_;
LogHandlerConfig config_;
};
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment