writer = make_shared<ImmediateFileWriter>(std::move(outputFile));
}
- return make_shared<StandardLogHandler>(formatter, writer);
+ return make_shared<StandardLogHandler>(
+ LogHandlerConfig{getType(), options}, formatter, writer);
}
} // namespace folly
#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>
// 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));
namespace folly {
class LogCategory;
+class LogHandlerConfig;
class LogMessage;
/**
* 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
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() {}
void StandardLogHandler::flush() {
writer_->flush();
}
+
+LogHandlerConfig StandardLogHandler::getConfig() const {
+ return config_;
+}
+
} // namespace folly
#include <folly/File.h>
#include <folly/Range.h>
#include <folly/experimental/logging/LogHandler.h>
+#include <folly/experimental/logging/LogHandlerConfig.h>
namespace folly {
class StandardLogHandler : public LogHandler {
public:
StandardLogHandler(
+ LogHandlerConfig config,
std::shared_ptr<LogFormatter> formatter,
std::shared_ptr<LogWriter> writer);
~StandardLogHandler();
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
* 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;
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");
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");
*/
#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 {
*/
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_;
}
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