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