diff --git a/libminifi/include/core/logging/LoggerConfiguration.h b/libminifi/include/core/logging/LoggerConfiguration.h index 7515bd7bee..9aed98df9c 100644 --- a/libminifi/include/core/logging/LoggerConfiguration.h +++ b/libminifi/include/core/logging/LoggerConfiguration.h @@ -116,13 +116,16 @@ class LoggerConfiguration { protected: static std::shared_ptr initialize_namespaces(const std::shared_ptr &logger_properties, const std::shared_ptr &logger = {}); - static std::shared_ptr get_logger(const std::shared_ptr &root_namespace, - std::string_view name_view, + static std::shared_ptr get_logger(const std::lock_guard&, + const std::shared_ptr &root_namespace, + const std::string& name, const std::shared_ptr& formatter); - static std::shared_ptr create_logger(const std::shared_ptr &root_namespace, + static std::shared_ptr create_logger(const std::lock_guard&, + const std::shared_ptr &root_namespace, const std::string& name, const std::shared_ptr& formatter); - static void setupSpdLogger(const std::shared_ptr& spd_logger, + static void setupSpdLogger(const std::lock_guard&, + const std::shared_ptr& spd_logger, const std::shared_ptr &root_namespace, const std::string& name, const std::shared_ptr& formatter); @@ -171,7 +174,7 @@ class LoggerConfiguration { LoggerId calculateLoggerId(std::string_view name, const std::optional& id) const; std::shared_ptr formatter_; - std::mutex mutex; + std::mutex mutex_; std::shared_ptr logger_ = nullptr; std::shared_ptr controller_; std::unordered_set> alert_sinks_; diff --git a/libminifi/src/core/logging/LoggerConfiguration.cpp b/libminifi/src/core/logging/LoggerConfiguration.cpp index 2d3987884a..a5f338e44b 100644 --- a/libminifi/src/core/logging/LoggerConfiguration.cpp +++ b/libminifi/src/core/logging/LoggerConfiguration.cpp @@ -91,12 +91,13 @@ std::vector LoggerProperties::get_keys_of_type(const std::string &t LoggerConfiguration::LoggerConfiguration() : root_namespace_(create_default_root()), formatter_(std::make_shared(spdlog_default_pattern)) { + const std::lock_guard lock(mutex_); controller_ = std::make_shared(); logger_ = std::make_shared( std::string(core::className()), std::nullopt, controller_, - get_logger(root_namespace_, core::className(), formatter_)); + get_logger(lock, root_namespace_, std::string(core::className()), formatter_)); } LoggerConfiguration& LoggerConfiguration::getConfiguration() { @@ -105,7 +106,7 @@ LoggerConfiguration& LoggerConfiguration::getConfiguration() { } void LoggerConfiguration::initialize(const std::shared_ptr &logger_properties) { - const std::lock_guard lock(mutex); + const std::lock_guard lock(mutex_); root_namespace_ = initialize_namespaces(logger_properties, logger_); alert_sinks_.clear(); root_namespace_->forEachSink([&] (const std::shared_ptr& sink) { @@ -144,13 +145,13 @@ void LoggerConfiguration::initialize(const std::shared_ptr &lo formatter_ = std::make_shared(spdlog_pattern); spdlog::apply_all([&](auto spd_logger) { - setupSpdLogger(spd_logger, root_namespace_, spd_logger->name(), formatter_); + setupSpdLogger(lock, spd_logger, root_namespace_, spd_logger->name(), formatter_); }); logger_->log_debug("Set following pattern on loggers: {}", spdlog_pattern); } std::shared_ptr LoggerConfiguration::getLogger(std::string_view name, const std::optional& id) { - const std::lock_guard lock(mutex); + const std::lock_guard lock(mutex_); return getLogger(name, id, lock); } @@ -166,10 +167,10 @@ LoggerConfiguration::LoggerId LoggerConfiguration::calculateLoggerId(std::string return LoggerId{.name = adjusted_name, .uuid = include_uuid_ ? id : std::nullopt}; } -std::shared_ptr LoggerConfiguration::getLogger(std::string_view name, const std::optional& id, const std::lock_guard& /*lock*/) { +std::shared_ptr LoggerConfiguration::getLogger(std::string_view name, const std::optional& id, const std::lock_guard& lock) { const auto logger_id = calculateLoggerId(name, id); - std::shared_ptr result = std::make_shared(logger_id.name, logger_id.uuid, controller_, get_logger(root_namespace_, logger_id.name, formatter_)); + std::shared_ptr result = std::make_shared(logger_id.name, logger_id.uuid, controller_, get_logger(lock, root_namespace_, logger_id.name, formatter_)); if (max_log_entry_length_) { result->set_max_log_size(gsl::narrow(*max_log_entry_length_)); } @@ -257,16 +258,18 @@ std::shared_ptr LoggerConfiguration::initialize_names return root_namespace; } -std::shared_ptr LoggerConfiguration::get_logger(const std::shared_ptr &root_namespace, const std::string_view name_view, - const std::shared_ptr& formatter) { - const std::string name{name_view}; +std::shared_ptr LoggerConfiguration::get_logger(const std::lock_guard& lock, + const std::shared_ptr &root_namespace, + const std::string& name, + const std::shared_ptr& formatter) { if (auto spdlogger = spdlog::get(name)) { return spdlogger; } - return create_logger(root_namespace, name, formatter); + return create_logger(lock, root_namespace, name, formatter); } -void LoggerConfiguration::setupSpdLogger(const std::shared_ptr& spd_logger, +void LoggerConfiguration::setupSpdLogger(const std::lock_guard&, + const std::shared_ptr& spd_logger, const std::shared_ptr& root_namespace, const std::string& name, const std::shared_ptr& formatter) { @@ -301,10 +304,10 @@ void LoggerConfiguration::setupSpdLogger(const std::shared_ptr& spd_logger->flush_on(std::max(spdlog::level::info, current_namespace->level)); } -std::shared_ptr LoggerConfiguration::create_logger(const std::shared_ptr& root_namespace, const std::string& name, +std::shared_ptr LoggerConfiguration::create_logger(const std::lock_guard& lock, const std::shared_ptr& root_namespace, const std::string& name, const std::shared_ptr& formatter) { const auto spd_logger = gsl::make_not_null(std::make_shared(name)); - setupSpdLogger(spd_logger, root_namespace, name, formatter); + setupSpdLogger(lock, spd_logger, root_namespace, name, formatter); try { spdlog::register_logger(spd_logger); } catch (const spdlog::spdlog_ex &) { @@ -344,7 +347,7 @@ void LoggerConfiguration::initializeCompression(const std::lock_guard& agent_id) { - std::lock_guard guard(mutex); + std::lock_guard guard(mutex_); for (auto& sink : alert_sinks_) { sink->initialize(controller, agent_id); }