Skip to content

Commit

Permalink
keep the UUID as an Identifier through the call chains, and only stri…
Browse files Browse the repository at this point in the history
…ngify it at the end
  • Loading branch information
fgerlits committed Jan 4, 2023
1 parent 90b951f commit 3ed5ce4
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions libminifi/include/core/logging/LoggerConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ struct LoggerNamespace {
void forEachSink(const std::function<void(const std::shared_ptr<spdlog::sinks::sink>&)>& op) const;
};

inline std::optional<std::string> formatId(std::optional<std::string> id) {
return std::move(id) | utils::map([](auto str){ return " (" + str + ")"; });
inline std::optional<std::string> formatId(std::optional<utils::Identifier> opt_id) {
return opt_id | utils::map([](auto id) { return " (" + std::string(id.to_string()) + ")"; });
}
} // namespace internal

Expand Down Expand Up @@ -109,7 +109,7 @@ class LoggerConfiguration {
/**
* Can be used to get arbitrarily named Logger, LoggerFactory should be preferred within a class.
*/
std::shared_ptr<Logger> getLogger(const std::string& name, const std::optional<std::string>& id = {});
std::shared_ptr<Logger> getLogger(const std::string& name, const std::optional<utils::Identifier>& id = {});

static const char *spdlog_default_pattern;

Expand All @@ -119,7 +119,7 @@ class LoggerConfiguration {
const std::shared_ptr<spdlog::formatter>& formatter, bool remove_if_present = false);

private:
std::shared_ptr<Logger> getLogger(const std::string& name, const std::optional<std::string>& id, const std::lock_guard<std::mutex>& lock);
std::shared_ptr<Logger> getLogger(const std::string& name, const std::optional<utils::Identifier>& id, const std::lock_guard<std::mutex>& lock);

void initializeCompression(const std::lock_guard<std::mutex>& lock, const std::shared_ptr<LoggerProperties>& properties);

Expand All @@ -132,10 +132,10 @@ class LoggerConfiguration {

class LoggerImpl : public Logger {
public:
explicit LoggerImpl(std::string name, std::optional<std::string> id, const std::shared_ptr<LoggerControl> &controller, const std::shared_ptr<spdlog::logger> &delegate)
explicit LoggerImpl(std::string name, std::optional<utils::Identifier> id, const std::shared_ptr<LoggerControl> &controller, const std::shared_ptr<spdlog::logger> &delegate)
: Logger(delegate, controller),
name(std::move(name)),
id(internal::formatId(std::move(id))) {
id(internal::formatId(id)) {
}

void set_delegate(std::shared_ptr<spdlog::logger> delegate) {
Expand Down
4 changes: 2 additions & 2 deletions libminifi/include/core/logging/LoggerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace org::apache::nifi::minifi::core::logging {

class LoggerFactoryBase {
public:
static std::shared_ptr<Logger> getAliasedLogger(const std::string& name, const std::optional<std::string>& id = {});
static std::shared_ptr<Logger> getAliasedLogger(const std::string& name, const std::optional<utils::Identifier>& id = {});
};

template<typename T>
Expand All @@ -40,7 +40,7 @@ class LoggerFactory : public LoggerFactoryBase {
}

static std::shared_ptr<Logger> getLogger(const utils::Identifier& uuid) {
return getAliasedLogger(core::getClassName<T>(), std::string{uuid.to_string()});
return getAliasedLogger(core::getClassName<T>(), uuid);
}
};

Expand Down
4 changes: 2 additions & 2 deletions libminifi/src/core/logging/LoggerConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ void LoggerConfiguration::initialize(const std::shared_ptr<LoggerProperties> &lo
logger_->log_debug("Set following pattern on loggers: %s", spdlog_pattern);
}

std::shared_ptr<Logger> LoggerConfiguration::getLogger(const std::string& name, const std::optional<std::string>& id) {
std::shared_ptr<Logger> LoggerConfiguration::getLogger(const std::string& name, const std::optional<utils::Identifier>& id) {
std::lock_guard<std::mutex> lock(mutex);
return getLogger(name, id, lock);
}

std::shared_ptr<Logger> LoggerConfiguration::getLogger(const std::string& name, const std::optional<std::string>& id, const std::lock_guard<std::mutex>& /*lock*/) {
std::shared_ptr<Logger> LoggerConfiguration::getLogger(const std::string& name, const std::optional<utils::Identifier>& id, const std::lock_guard<std::mutex>& /*lock*/) {
std::string adjusted_name = name;
const std::string clazz = "class ";
auto haz_clazz = name.find(clazz);
Expand Down
2 changes: 1 addition & 1 deletion libminifi/src/core/logging/LoggerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace org::apache::nifi::minifi::core::logging {

std::shared_ptr<Logger> LoggerFactoryBase::getAliasedLogger(const std::string& name, const std::optional<std::string>& id) {
std::shared_ptr<Logger> LoggerFactoryBase::getAliasedLogger(const std::string& name, const std::optional<utils::Identifier>& id) {
return LoggerConfiguration::getConfiguration().getLogger(name, id);
}

Expand Down
2 changes: 1 addition & 1 deletion libminifi/test/TestBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void LogTestController::setLevel(const std::string& name, spdlog::level::level_e
logging::LoggerConfiguration::getSpdlogLogger(adjusted_name)->set_level(level);
}

std::shared_ptr<logging::Logger> LogTestController::getLoggerByClassName(const std::string& class_name, const std::optional<std::string>& id) {
std::shared_ptr<logging::Logger> LogTestController::getLoggerByClassName(const std::string& class_name, const std::optional<utils::Identifier>& id) {
return config ? config->getLogger(class_name, id) : logging::LoggerConfiguration::getConfiguration().getLogger(class_name, id);
}

Expand Down
4 changes: 2 additions & 2 deletions libminifi/test/TestBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ class LogTestController {
* of changeable test formats
*/
template<typename T>
std::shared_ptr<logging::Logger> getLogger(const std::optional<std::string>& id = {}) { return getLoggerByClassName(minifi::core::getClassName<T>(), id); }
std::shared_ptr<logging::Logger> getLogger(const std::optional<utils::Identifier>& id = {}) { return getLoggerByClassName(minifi::core::getClassName<T>(), id); }

std::shared_ptr<logging::Logger> getLoggerByClassName(const std::string& class_name, const std::optional<std::string>& id = {});
std::shared_ptr<logging::Logger> getLoggerByClassName(const std::string& class_name, const std::optional<utils::Identifier>& id = {});

template<typename T>
void setLevel(spdlog::level::level_enum level) {
Expand Down
2 changes: 1 addition & 1 deletion libminifi/test/unit/LoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TEST_CASE("Printing of the ID can be disabled in the config", "[logger][id][conf
}

const auto uuid = utils::IdGenerator::getIdGenerator()->generate();
std::shared_ptr<logging::Logger> logger = LogTestController::getInstance(properties)->getLogger<logging::Logger>(std::string{uuid.to_string()});
std::shared_ptr<logging::Logger> logger = LogTestController::getInstance(properties)->getLogger<logging::Logger>(uuid);
logger->log_error("hello %s", "world");

CHECK(LogTestController::getInstance().contains("[org::apache::nifi::minifi::core::logging::Logger] [error] hello world"));
Expand Down

0 comments on commit 3ed5ce4

Please sign in to comment.