Turn logging off by default, allow turning on via config/cmdline (#295)

* Turn logging off by default, allow turning on via config/cmdline
* No need to store config in member variable if things are decided at construction time
This commit is contained in:
Jerin Philip 2022-01-02 00:17:12 +00:00 committed by GitHub
parent d209e4fc49
commit ddccc77570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 3 deletions

View File

@ -7,9 +7,45 @@ namespace bergamot {
// RAII Wrap around logging, to clean up after the object on stack.
class Logger {
public:
Logger() : marianLoggers_(createLoggers()) {
struct Config {
std::string level{"off"};
template <class App>
static void addOptions(App &app, Config &config) {
app.add_option("--log-level", config.level,
"Set verbosity level of logging: trace, debug, info, warn, err(or), critical, off");
}
};
Logger(const Config &config) : marianLoggers_(createLoggers()) {
// We are manually creating loggers, because this is usually created in marian as a side-effect of
// config-parsing.
for (auto &logger : marianLoggers_) {
setLoggingLevel(*logger, config.level);
}
}
// Taken from
// https://github.com/marian-nmt/marian-dev/blob/c84599d08ad69059279abd5a7417a8053db8b631/src/common/logging.cpp#L45
static bool setLoggingLevel(spdlog::logger &logger, std::string const level) {
if (level == "trace")
logger.set_level(spdlog::level::trace);
else if (level == "debug")
logger.set_level(spdlog::level::debug);
else if (level == "info")
logger.set_level(spdlog::level::info);
else if (level == "warn")
logger.set_level(spdlog::level::warn);
else if (level == "err" || level == "error")
logger.set_level(spdlog::level::err);
else if (level == "critical")
logger.set_level(spdlog::level::critical);
else if (level == "off")
logger.set_level(spdlog::level::off);
else {
logger.warn("Unknown log level '{}' for logger '{}'", level.c_str(), logger.name().c_str());
return false;
}
return true;
}
~Logger() {

View File

@ -11,7 +11,11 @@ namespace marian {
namespace bergamot {
BlockingService::BlockingService(const BlockingService::Config &config)
: config_(config), requestId_(0), batchingPool_(), cache_(config.cacheSize, /*mutexBuckets=*/1) {}
: config_(config),
requestId_(0),
batchingPool_(),
cache_(config.cacheSize, /*mutexBuckets=*/1),
logger_(config.logger) {}
std::vector<Response> BlockingService::translateMultiple(std::shared_ptr<TranslationModel> translationModel,
std::vector<std::string> &&sources,
@ -37,7 +41,11 @@ std::vector<Response> BlockingService::translateMultiple(std::shared_ptr<Transla
}
AsyncService::AsyncService(const AsyncService::Config &config)
: requestId_(0), config_(config), safeBatchingPool_(), cache_(config_.cacheSize, config_.cacheMutexBuckets) {
: requestId_(0),
config_(config),
safeBatchingPool_(),
cache_(config_.cacheSize, config_.cacheMutexBuckets),
logger_(config.logger) {
ABORT_IF(config_.numWorkers == 0, "Number of workers should be at least 1 in a threaded workflow");
workers_.reserve(config_.numWorkers);
for (size_t cpuId = 0; cpuId < config_.numWorkers; cpuId++) {

View File

@ -33,11 +33,14 @@ class BlockingService {
bool cacheEnabled{false}; ///< Whether to enable cache or not.
size_t cacheSize{2000}; ///< Size in History items to be stored in the cache. Loosely corresponds to sentences to
/// cache in the real world.
Logger::Config logger; // Configurations for logging
template <class App>
static void addOptions(App &app, Config &config) {
// Options will come here.
app.add_option("--cache-translations", config.cacheEnabled, "Whether to cache translations or not.");
app.add_option("--cache-size", config.cacheSize, "Number of entries to store in cache.");
Logger::Config::addOptions(app, config.logger);
}
};
/// Construct a BlockingService with configuration loaded from an Options object. Does not require any keys, values to
@ -90,6 +93,8 @@ class AsyncService {
size_t cacheMutexBuckets{1}; ///< Controls the granularity of locking to reduce contention by bucketing mutexes
///< guarding cache entry read write. Optimal at min(core, numWorkers) assuming a
///< reasonably large cache-size.
Logger::Config logger; // Configurations for logging
template <class App>
static void addOptions(App &app, Config &config) {
app.add_option("--cpu-threads", config.numWorkers, "Workers to form translation backend");
@ -97,6 +102,7 @@ class AsyncService {
app.add_option("--cache-size", config.cacheSize, "Number of entries to store in cache.");
app.add_option("--cache-mutex-buckets", config.cacheMutexBuckets,
"Number of mutex buckets to control locking granularity");
Logger::Config::addOptions(app, config.logger);
}
};
/// Construct an AsyncService with configuration loaded from Options. Expects positive integer value for