This is an automated email from the ASF dual-hosted git repository.
szaszm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 2da0ff4a2 MINIFICPP-2288 Remove the caching of loggers (spdlog will do
this for us anyway)
2da0ff4a2 is described below
commit 2da0ff4a26ef3afb058828282573e470c70d3c58
Author: Martin Zink <[email protected]>
AuthorDate: Fri Jan 19 11:17:57 2024 +0100
MINIFICPP-2288 Remove the caching of loggers (spdlog will do this for us
anyway)
Closes #1718
Signed-off-by: Marton Szasz <[email protected]>
---
extensions/sftp/tests/FetchSFTPTests.cpp | 15 ++-
extensions/sftp/tests/ListSFTPTests.cpp | 15 ++-
extensions/sftp/tests/ListThenFetchSFTPTests.cpp | 17 ++--
extensions/sftp/tests/PutSFTPTests.cpp | 44 ++++----
.../include/core/logging/LoggerConfiguration.h | 33 ++++--
libminifi/include/core/logging/LoggerProperties.h | 23 ++---
libminifi/src/core/logging/LoggerConfiguration.cpp | 111 ++++++++++-----------
libminifi/test/TestBase.cpp | 10 +-
libminifi/test/TestBase.h | 8 +-
libminifi/test/unit/LoggerConfigurationTests.cpp | 67 +------------
libminifi/test/unit/LoggerTests.cpp | 11 +-
minifi_main/MiNiFiMain.cpp | 28 +++---
12 files changed, 155 insertions(+), 227 deletions(-)
diff --git a/extensions/sftp/tests/FetchSFTPTests.cpp
b/extensions/sftp/tests/FetchSFTPTests.cpp
index c8a26ef12..32202e22a 100644
--- a/extensions/sftp/tests/FetchSFTPTests.cpp
+++ b/extensions/sftp/tests/FetchSFTPTests.cpp
@@ -38,6 +38,7 @@
class FetchSFTPTestsFixture {
public:
FetchSFTPTestsFixture() {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().setTrace<TestPlan>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setDebug<minifi::SchedulingAgent>();
@@ -110,12 +111,10 @@ class FetchSFTPTestsFixture {
FetchSFTPTestsFixture& operator=(FetchSFTPTestsFixture&&) = delete;
FetchSFTPTestsFixture& operator=(const FetchSFTPTestsFixture&) = delete;
- virtual ~FetchSFTPTestsFixture() {
- LogTestController::getInstance().reset();
- }
+ virtual ~FetchSFTPTestsFixture() = default;
// Create source file
- void createFile(const std::string& relative_path, const std::string&
content) {
+ void createFile(const std::string& relative_path, const std::string&
content) const {
const auto file_path = src_dir / "vfs" / relative_path;
std::filesystem::create_directories(file_path.parent_path());
@@ -130,8 +129,8 @@ class FetchSFTPTestsFixture {
IN_SOURCE
};
- void testFile(TestWhere where, const std::filesystem::path& relative_path,
std::string_view expected_content) {
- std::filesystem::path expected_path = where == IN_DESTINATION ? dst_dir /
relative_path : src_dir / "vfs" / relative_path;
+ void testFile(TestWhere where, const std::filesystem::path& relative_path,
std::string_view expected_content) const {
+ const auto expected_path = where == IN_DESTINATION ? dst_dir /
relative_path : src_dir / "vfs" / relative_path;
REQUIRE(std::filesystem::exists(expected_path));
std::filesystem::permissions(expected_path,
static_cast<std::filesystem::perms>(0644));
@@ -146,8 +145,8 @@ class FetchSFTPTestsFixture {
CHECK(expected_content == content.str());
}
- void testFileNotExists(TestWhere where, const std::string& relative_path) {
- std::filesystem::path expected_path = where == IN_DESTINATION ? dst_dir /
relative_path : src_dir / "vfs" / relative_path;
+ void testFileNotExists(TestWhere where, const std::string& relative_path)
const {
+ const auto expected_path = where == IN_DESTINATION ? dst_dir /
relative_path : src_dir / "vfs" / relative_path;
CHECK(!std::filesystem::exists(expected_path));
}
diff --git a/extensions/sftp/tests/ListSFTPTests.cpp
b/extensions/sftp/tests/ListSFTPTests.cpp
index 1e1fb2c0a..6f038d777 100644
--- a/extensions/sftp/tests/ListSFTPTests.cpp
+++ b/extensions/sftp/tests/ListSFTPTests.cpp
@@ -39,7 +39,6 @@
#include "TestBase.h"
#include "Catch.h"
-#include "utils/StringUtils.h"
#include "utils/file/FileUtils.h"
#include "core/Core.h"
#include "core/logging/Logger.h"
@@ -50,7 +49,6 @@
#include "processors/ListSFTP.h"
#include "processors/GenerateFlowFile.h"
#include "processors/LogAttribute.h"
-#include "processors/UpdateAttribute.h"
#include "tools/SFTPTestServer.h"
#include "utils/TestUtils.h"
@@ -59,6 +57,7 @@ using namespace std::literals::chrono_literals;
class ListSFTPTestsFixture {
public:
explicit ListSFTPTestsFixture(const std::shared_ptr<minifi::Configure>&
configuration = nullptr) {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().setTrace<TestPlan>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setDebug<minifi::SchedulingAgent>();
@@ -89,11 +88,9 @@ class ListSFTPTestsFixture {
ListSFTPTestsFixture& operator=(ListSFTPTestsFixture&&) = delete;
ListSFTPTestsFixture& operator=(const ListSFTPTestsFixture&) = delete;
- virtual ~ListSFTPTestsFixture() {
- LogTestController::getInstance().reset();
- }
+ virtual ~ListSFTPTestsFixture() = default;
- void createPlan(utils::Identifier* list_sftp_uuid = nullptr, const
std::shared_ptr<minifi::Configure>& configuration = nullptr) {
+ void createPlan(const utils::Identifier* list_sftp_uuid = nullptr, const
std::shared_ptr<minifi::Configure>& configuration = nullptr) {
const auto state_dir = plan == nullptr ?
testController.createTempDirectory() : plan->getStateDir();
log_attribute.reset();
@@ -144,9 +141,9 @@ class ListSFTPTestsFixture {
}
// Create source file
- void createFile(const std::filesystem::path& relative_path, const
std::string& content, std::optional<std::chrono::file_clock::time_point>
modification_time) {
+ void createFile(const std::filesystem::path& relative_path, const
std::string& content, const std::optional<std::chrono::file_clock::time_point>&
modification_time) const {
std::fstream file;
- std::filesystem::path full_path = working_directory / "vfs" /
relative_path;
+ const auto full_path = working_directory / "vfs" / relative_path;
std::filesystem::create_directories(full_path.parent_path());
file.open(full_path, std::ios::out);
file << content;
@@ -156,7 +153,7 @@ class ListSFTPTestsFixture {
}
}
- void createFileWithModificationTimeDiff(const std::filesystem::path&
relative_path, const std::string& content, std::chrono::seconds
modification_timediff = -5min) {
+ void createFileWithModificationTimeDiff(const std::filesystem::path&
relative_path, const std::string& content, std::chrono::seconds
modification_timediff = -5min) const {
return createFile(relative_path, content, std::chrono::file_clock::now() +
modification_timediff);
}
diff --git a/extensions/sftp/tests/ListThenFetchSFTPTests.cpp
b/extensions/sftp/tests/ListThenFetchSFTPTests.cpp
index 854cb1076..db8c30ece 100644
--- a/extensions/sftp/tests/ListThenFetchSFTPTests.cpp
+++ b/extensions/sftp/tests/ListThenFetchSFTPTests.cpp
@@ -36,19 +36,15 @@
#include "TestBase.h"
#include "Catch.h"
-#include "utils/StringUtils.h"
#include "utils/file/FileUtils.h"
-#include "core/Core.h"
#include "core/logging/Logger.h"
#include "core/ProcessGroup.h"
#include "FlowController.h"
-#include "properties/Configure.h"
#include "unit/ProvenanceTestHelper.h"
#include "processors/FetchSFTP.h"
#include "processors/ListSFTP.h"
#include "processors/GenerateFlowFile.h"
#include "processors/LogAttribute.h"
-#include "processors/UpdateAttribute.h"
#include "processors/PutFile.h"
#include "tools/SFTPTestServer.h"
@@ -57,6 +53,7 @@ using namespace std::literals::chrono_literals;
class ListThenFetchSFTPTestsFixture {
public:
ListThenFetchSFTPTestsFixture() {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().setTrace<TestPlan>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setDebug<minifi::SchedulingAgent>();
@@ -147,14 +144,12 @@ class ListThenFetchSFTPTestsFixture {
ListThenFetchSFTPTestsFixture& operator=(ListThenFetchSFTPTestsFixture&&) =
delete;
ListThenFetchSFTPTestsFixture& operator=(const
ListThenFetchSFTPTestsFixture&) = delete;
- virtual ~ListThenFetchSFTPTestsFixture() {
- LogTestController::getInstance().reset();
- }
+ virtual ~ListThenFetchSFTPTestsFixture() = default;
// Create source file
- void createFile(const std::string& relative_path, const std::string&
content, std::optional<std::chrono::file_clock::time_point> modification_time) {
+ void createFile(const std::string& relative_path, const std::string&
content, const std::optional<std::chrono::file_clock::time_point>&
modification_time) const {
std::fstream file;
- std::filesystem::path full_path = src_dir / "vfs" / relative_path;
+ const auto full_path = src_dir / "vfs" / relative_path;
std::filesystem::create_directories(full_path.parent_path());
file.open(full_path, std::ios::out);
file << content;
@@ -164,7 +159,7 @@ class ListThenFetchSFTPTestsFixture {
}
}
- void createFileWithModificationTimeDiff(const std::string& relative_path,
const std::string& content, std::chrono::seconds modification_timediff = -5min)
{
+ void createFileWithModificationTimeDiff(const std::string& relative_path,
const std::string& content, std::chrono::seconds modification_timediff = -5min)
const {
return createFile(relative_path, content, std::chrono::file_clock::now() +
modification_timediff);
}
@@ -173,7 +168,7 @@ class ListThenFetchSFTPTestsFixture {
IN_SOURCE
};
- void testFile(TestWhere where, const std::filesystem::path& relative_path,
std::string_view expected_content) {
+ void testFile(TestWhere where, const std::filesystem::path& relative_path,
std::string_view expected_content) const {
std::filesystem::path expected_path = where == IN_DESTINATION ? dst_dir /
relative_path : src_dir / "vfs" / relative_path;
REQUIRE(std::filesystem::exists(expected_path));
std::filesystem::permissions(expected_path,
static_cast<std::filesystem::perms>(0644));;
diff --git a/extensions/sftp/tests/PutSFTPTests.cpp
b/extensions/sftp/tests/PutSFTPTests.cpp
index e7904873f..034145f5e 100644
--- a/extensions/sftp/tests/PutSFTPTests.cpp
+++ b/extensions/sftp/tests/PutSFTPTests.cpp
@@ -41,19 +41,16 @@
#include "Catch.h"
#include "Exception.h"
#include "date/date.h"
-#include "utils/StringUtils.h"
#include "utils/file/FileUtils.h"
#include "core/Core.h"
#include "core/logging/Logger.h"
#include "core/ProcessGroup.h"
#include "FlowController.h"
-#include "properties/Configure.h"
#include "unit/ProvenanceTestHelper.h"
#include "processors/PutSFTP.h"
#include "processors/GetFile.h"
#include "processors/LogAttribute.h"
#include "processors/ExtractText.h"
-#include "processors/UpdateAttribute.h"
#include "tools/SFTPTestServer.h"
constexpr const char* PUBLIC_KEY_AUTH_ERROR_MESSAGE = "Failed to authenticate
with publickey, error: Unable to extract public key from private key file:
Wrong passphrase or invalid/unrecognized private key file format"; //
NOLINT(whitespace/line_length)
@@ -62,6 +59,7 @@ using namespace std::literals::chrono_literals;
class PutSFTPTestsFixture {
public:
PutSFTPTestsFixture() {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().setTrace<TestPlan>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setDebug<minifi::SchedulingAgent>();
@@ -124,9 +122,7 @@ class PutSFTPTestsFixture {
PutSFTPTestsFixture& operator=(PutSFTPTestsFixture&&) = delete;
PutSFTPTestsFixture& operator=(const PutSFTPTestsFixture&) = delete;
- virtual ~PutSFTPTestsFixture() {
- LogTestController::getInstance().reset();
- }
+ virtual ~PutSFTPTestsFixture() = default;
// Create source file
static void createFile(const std::string &dir, const std::string&
relative_path, const std::string& content) {
@@ -139,7 +135,7 @@ class PutSFTPTestsFixture {
}
// Test target file
- void testFile(const std::string& relative_path, const std::string&
expected_content) {
+ void testFile(const std::string& relative_path, const std::string&
expected_content) const {
auto result_file = dst_dir / "vfs" / relative_path;
std::ifstream file(result_file);
REQUIRE(true == file.good());
@@ -152,27 +148,27 @@ class PutSFTPTestsFixture {
REQUIRE(expected_content == content.str());
}
- void testFileNotExists(const std::string& relative_path) {
+ void testFileNotExists(const std::string& relative_path) const {
auto result_file = dst_dir / "vfs" / relative_path;
std::ifstream file(result_file);
REQUIRE(false == file.is_open());
REQUIRE(false == file.good());
}
- void testModificationTime(const std::string& relative_path,
std::chrono::file_clock::time_point mtime) {
- auto result_file = dst_dir / "vfs" / relative_path;
+ void testModificationTime(const std::string& relative_path,
std::chrono::file_clock::time_point mtime) const {
+ const auto result_file = dst_dir / "vfs" / relative_path;
REQUIRE(mtime == utils::file::last_write_time(result_file).value());
}
- void testPermissions(const std::string& relative_path, uint32_t
expected_permissions) {
- auto result_file = dst_dir / "vfs" / relative_path;
+ void testPermissions(const std::string& relative_path, uint32_t
expected_permissions) const {
+ const auto result_file = dst_dir / "vfs" / relative_path;
uint32_t permissions = 0U;
REQUIRE(true == utils::file::get_permissions(result_file, permissions));
REQUIRE(expected_permissions == permissions);
}
- void testOwner(const std::string& relative_path, uint64_t expected_uid) {
- auto result_file = dst_dir / "vfs" / relative_path;
+ void testOwner(const std::string& relative_path, uint64_t expected_uid)
const {
+ const auto result_file = dst_dir / "vfs" / relative_path;
uint64_t uid = 0U;
uint64_t gid = 0U;
@@ -180,8 +176,8 @@ class PutSFTPTestsFixture {
REQUIRE(expected_uid == uid);
}
- void testGroup(const std::string& relative_path, uint64_t expected_gid) {
- auto result_file = dst_dir / "vfs" / relative_path;
+ void testGroup(const std::string& relative_path, uint64_t expected_gid)
const {
+ const auto result_file = dst_dir / "vfs" / relative_path;
uint64_t uid = 0U;
uint64_t gid = 0U;
@@ -230,7 +226,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP bad
password", "[PutSFTP][authent
try {
testController.runSession(plan, true);
} catch (std::exception &e) {
- std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
+ const std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
REQUIRE(0 == std::string(e.what()).compare(0, expected.size(), expected));
}
@@ -260,7 +256,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP public key
authentication bad pas
try {
testController.runSession(plan, true);
} catch (std::exception &e) {
- std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
+ const std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
REQUIRE(0 == std::string(e.what()).compare(0, expected.size(), expected));
}
REQUIRE(LogTestController::getInstance().contains(PUBLIC_KEY_AUTH_ERROR_MESSAGE));
@@ -303,7 +299,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP host key
checking missing strict"
try {
testController.runSession(plan, true);
} catch (std::exception &e) {
- std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
+ const std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
REQUIRE(0 == std::string(e.what()).compare(0, expected.size(), expected));
}
@@ -333,7 +329,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP host key
checking mismatch strict
try {
testController.runSession(plan, true);
} catch (std::exception &e) {
- std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
+ const std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
REQUIRE(0 == std::string(e.what()).compare(0, expected.size(), expected));
}
@@ -501,7 +497,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP set mtime",
"[PutSFTP]") {
testController.runSession(plan, true);
testFile("nifi_test/tstFile1.ext", "content 1");
- std::chrono::system_clock::time_point modification_time =
date::sys_days(date::January / 24 / 2065) + 5h + 20min;
+ constexpr auto modification_time = date::sys_days(date::January / 24 / 2065)
+ 5h + 20min;
testModificationTime("nifi_test/tstFile1.ext",
utils::file::from_sys(modification_time));
}
@@ -663,13 +659,13 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP connection
caching does not reuse
createFile(src_dir, "tstFile1.ext", "content 1");
/* Simulate connection failure */
- auto port = sftp_server->getPort();
+ const auto port = sftp_server->getPort();
sftp_server.reset();
try {
testController.runSession(plan, true);
} catch (std::exception &e) {
- std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
+ const std::string expected =
minifi::Exception(minifi::PROCESS_SESSION_EXCEPTION, "Can not find the transfer
relationship for the updated flow").what();
REQUIRE(0 == std::string(e.what()).compare(0, expected.size(), expected));
}
@@ -815,7 +811,7 @@ TEST_CASE_METHOD(PutSFTPTestsFixture, "PutSFTP expression
language test", "[PutS
get_file = plan->addProcessor(
"GetFile",
"GetFile");
- auto update_attribute = plan->addProcessor(
+ const auto update_attribute = plan->addProcessor(
"UpdateAttribute",
"UpdateAttribute",
core::Relationship("success", "d"),
diff --git a/libminifi/include/core/logging/LoggerConfiguration.h
b/libminifi/include/core/logging/LoggerConfiguration.h
index a34d68bd0..9aed98df9 100644
--- a/libminifi/include/core/logging/LoggerConfiguration.h
+++ b/libminifi/include/core/logging/LoggerConfiguration.h
@@ -34,13 +34,11 @@
#include "spdlog/sinks/sink.h"
#include "spdlog/logger.h"
#include "spdlog/formatter.h"
-#include "spdlog/pattern_formatter.h"
#include "core/Core.h"
#include "core/logging/Logger.h"
#include "LoggerProperties.h"
#include "internal/CompressionManager.h"
-#include "core/logging/LoggerFactory.h"
#include "alert/AlertSink.h"
class LoggerTestAccessor;
@@ -61,7 +59,7 @@ 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<utils::Identifier>
opt_id) {
+inline std::optional<std::string> formatId(const
std::optional<utils::Identifier>& opt_id) {
return opt_id | utils::transform([](auto id) { return " (" +
std::string(id.to_string()) + ")"; });
}
@@ -118,8 +116,19 @@ class LoggerConfiguration {
protected:
static std::shared_ptr<internal::LoggerNamespace>
initialize_namespaces(const std::shared_ptr<LoggerProperties>
&logger_properties, const std::shared_ptr<Logger> &logger = {});
- static std::shared_ptr<spdlog::logger> get_logger(const
std::shared_ptr<Logger>& logger, const
std::shared_ptr<internal::LoggerNamespace> &root_namespace, std::string_view
name_view,
- const
std::shared_ptr<spdlog::formatter>& formatter, bool remove_if_present = false);
+ static std::shared_ptr<spdlog::logger> get_logger(const
std::lock_guard<std::mutex>&,
+ const std::shared_ptr<internal::LoggerNamespace> &root_namespace,
+ const std::string& name,
+ const std::shared_ptr<spdlog::formatter>& formatter);
+ static std::shared_ptr<spdlog::logger> create_logger(const
std::lock_guard<std::mutex>&,
+ const std::shared_ptr<internal::LoggerNamespace> &root_namespace,
+ const std::string& name,
+ const std::shared_ptr<spdlog::formatter>& formatter);
+ static void setupSpdLogger(const std::lock_guard<std::mutex>&,
+ const std::shared_ptr<spdlog::logger>& spd_logger,
+ const std::shared_ptr<internal::LoggerNamespace> &root_namespace,
+ const std::string& name,
+ const std::shared_ptr<spdlog::formatter>& formatter);
private:
std::shared_ptr<Logger> getLogger(std::string_view name, const
std::optional<utils::Identifier>& id, const std::lock_guard<std::mutex>& lock);
@@ -135,9 +144,9 @@ class LoggerConfiguration {
class LoggerImpl : public Logger {
public:
- explicit LoggerImpl(std::string_view name,
std::optional<utils::Identifier> id, const std::shared_ptr<LoggerControl>
&controller, const std::shared_ptr<spdlog::logger> &delegate)
+ explicit LoggerImpl(std::string name, const
std::optional<utils::Identifier>& id, const std::shared_ptr<LoggerControl>
&controller, const std::shared_ptr<spdlog::logger> &delegate)
: Logger(delegate, controller),
- name(name),
+ name(std::move(name)),
id(internal::formatId(id)) {
}
@@ -157,9 +166,15 @@ class LoggerConfiguration {
LoggerConfiguration();
internal::CompressionManager compression_manager_;
std::shared_ptr<internal::LoggerNamespace> root_namespace_;
- std::vector<std::shared_ptr<LoggerImpl>> loggers;
+
+ struct LoggerId {
+ std::string name;
+ std::optional<utils::Identifier> uuid;
+ };
+ LoggerId calculateLoggerId(std::string_view name, const
std::optional<utils::Identifier>& id) const;
+
std::shared_ptr<spdlog::formatter> formatter_;
- std::mutex mutex;
+ std::mutex mutex_;
std::shared_ptr<LoggerImpl> logger_ = nullptr;
std::shared_ptr<LoggerControl> controller_;
std::unordered_set<std::shared_ptr<AlertSink>> alert_sinks_;
diff --git a/libminifi/include/core/logging/LoggerProperties.h
b/libminifi/include/core/logging/LoggerProperties.h
index 1341a7f00..d94f186f1 100644
--- a/libminifi/include/core/logging/LoggerProperties.h
+++ b/libminifi/include/core/logging/LoggerProperties.h
@@ -27,12 +27,7 @@
#include "properties/Properties.h"
-namespace org {
-namespace apache {
-namespace nifi {
-namespace minifi {
-namespace core {
-namespace logging {
+namespace org::apache::nifi::minifi::core::logging {
class LoggerProperties : public Properties {
public:
@@ -45,12 +40,12 @@ class LoggerProperties : public Properties {
* Ex: with type argument "appender"
* you would get back a property of "appender.rolling" but not
"appender.rolling.file_name"
*/
- std::vector<std::string> get_keys_of_type(const std::string &type);
+ std::vector<std::string> get_keys_of_type(const std::string &type) const;
/**
* Registers a sink witht the given name. This allows for programmatic
definition of sinks.
*/
- void add_sink(const std::string &name, std::shared_ptr<spdlog::sinks::sink>
sink) {
+ void add_sink(const std::string &name, const
std::shared_ptr<spdlog::sinks::sink>& sink) {
sinks_[name] = sink;
}
std::map<std::string, std::shared_ptr<spdlog::sinks::sink>> initial_sinks() {
@@ -61,9 +56,9 @@ class LoggerProperties : public Properties {
std::map<std::string, std::shared_ptr<spdlog::sinks::sink>> sinks_;
};
-} // namespace logging
-} // namespace core
-} // namespace minifi
-} // namespace nifi
-} // namespace apache
-} // namespace org
+} // namespace org::apache::nifi::minifi::core::logging
+
+
+
+
+
diff --git a/libminifi/src/core/logging/LoggerConfiguration.cpp
b/libminifi/src/core/logging/LoggerConfiguration.cpp
index 232020942..a5f338e44 100644
--- a/libminifi/src/core/logging/LoggerConfiguration.cpp
+++ b/libminifi/src/core/logging/LoggerConfiguration.cpp
@@ -22,11 +22,9 @@
#include <sys/stat.h>
#include <algorithm>
-#include <atomic>
#include <map>
#include <memory>
#include <optional>
-#include <queue>
#include <string>
#include <vector>
@@ -44,6 +42,7 @@
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_sinks.h"
#include "spdlog/sinks/null_sink.h"
+#include "spdlog/pattern_formatter.h"
#ifdef WIN32
#include "core/logging/WindowsEventLogSink.h"
@@ -78,9 +77,9 @@ void LoggerNamespace::forEachSink(const
std::function<void(const std::shared_ptr
} // namespace internal
-std::vector<std::string> LoggerProperties::get_keys_of_type(const std::string
&type) {
+std::vector<std::string> LoggerProperties::get_keys_of_type(const std::string
&type) const {
std::vector<std::string> appenders;
- std::string prefix = type + ".";
+ const std::string prefix = type + ".";
for (auto const & entry : getProperties()) {
if (entry.first.rfind(prefix, 0) == 0 && entry.first.find(".",
prefix.length() + 1) == std::string::npos) {
appenders.push_back(entry.first);
@@ -92,13 +91,13 @@ std::vector<std::string>
LoggerProperties::get_keys_of_type(const std::string &t
LoggerConfiguration::LoggerConfiguration()
: root_namespace_(create_default_root()),
formatter_(std::make_shared<spdlog::pattern_formatter>(spdlog_default_pattern))
{
+ const std::lock_guard<std::mutex> lock(mutex_);
controller_ = std::make_shared<LoggerControl>();
logger_ = std::make_shared<LoggerImpl>(
- core::className<LoggerConfiguration>(),
+ std::string(core::className<LoggerConfiguration>()),
std::nullopt,
controller_,
- get_logger(nullptr, root_namespace_,
core::className<LoggerConfiguration>(), formatter_));
- loggers.push_back(logger_);
+ get_logger(lock, root_namespace_,
std::string(core::className<LoggerConfiguration>()), formatter_));
}
LoggerConfiguration& LoggerConfiguration::getConfiguration() {
@@ -107,7 +106,7 @@ LoggerConfiguration&
LoggerConfiguration::getConfiguration() {
}
void LoggerConfiguration::initialize(const std::shared_ptr<LoggerProperties>
&logger_properties) {
- std::lock_guard<std::mutex> lock(mutex);
+ const std::lock_guard<std::mutex> lock(mutex_);
root_namespace_ = initialize_namespaces(logger_properties, logger_);
alert_sinks_.clear();
root_namespace_->forEachSink([&] (const
std::shared_ptr<spdlog::sinks::sink>& sink) {
@@ -145,40 +144,33 @@ void LoggerConfiguration::initialize(const
std::shared_ptr<LoggerProperties> &lo
}
formatter_ = std::make_shared<spdlog::pattern_formatter>(spdlog_pattern);
- std::map<std::string, std::shared_ptr<spdlog::logger>> spdloggers;
- for (auto const & logger_impl : loggers) {
- std::shared_ptr<spdlog::logger> spdlogger;
- auto it = spdloggers.find(logger_impl->name);
- if (it == spdloggers.end()) {
- spdlogger = get_logger(logger_, root_namespace_, logger_impl->name,
formatter_, true);
- spdloggers[logger_impl->name] = spdlogger;
- } else {
- spdlogger = it->second;
- }
- logger_impl->set_delegate(spdlogger);
- }
+ spdlog::apply_all([&](auto spd_logger) {
+ setupSpdLogger(lock, spd_logger, root_namespace_, spd_logger->name(),
formatter_);
+ });
logger_->log_debug("Set following pattern on loggers: {}", spdlog_pattern);
}
std::shared_ptr<Logger> LoggerConfiguration::getLogger(std::string_view name,
const std::optional<utils::Identifier>& id) {
- std::lock_guard<std::mutex> lock(mutex);
+ const std::lock_guard<std::mutex> lock(mutex_);
return getLogger(name, id, lock);
}
-std::shared_ptr<Logger> LoggerConfiguration::getLogger(std::string_view name,
const std::optional<utils::Identifier>& id, const std::lock_guard<std::mutex>&
/*lock*/) {
+LoggerConfiguration::LoggerId
LoggerConfiguration::calculateLoggerId(std::string_view name, const
std::optional<utils::Identifier>& id) const {
std::string adjusted_name{name};
const std::string clazz = "class ";
- auto haz_clazz = name.find(clazz);
+ const auto haz_clazz = name.find(clazz);
if (haz_clazz == 0)
adjusted_name = name.substr(clazz.length(), name.length() -
clazz.length());
if (shorten_names_) {
utils::ClassUtils::shortenClassName(adjusted_name, adjusted_name);
}
+ return LoggerId{.name = adjusted_name, .uuid = include_uuid_ ? id :
std::nullopt};
+}
- const auto id_if_enabled = include_uuid_ ? id : std::nullopt;
+std::shared_ptr<Logger> LoggerConfiguration::getLogger(std::string_view name,
const std::optional<utils::Identifier>& id, const std::lock_guard<std::mutex>&
lock) {
+ const auto logger_id = calculateLoggerId(name, id);
- std::shared_ptr<LoggerImpl> result =
std::make_shared<LoggerImpl>(adjusted_name, id_if_enabled, controller_,
get_logger(logger_, root_namespace_, adjusted_name, formatter_));
- loggers.push_back(result);
+ std::shared_ptr<LoggerImpl> result =
std::make_shared<LoggerImpl>(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<int>(*max_log_entry_length_));
}
@@ -199,7 +191,7 @@ std::shared_ptr<internal::LoggerNamespace>
LoggerConfiguration::initialize_names
if (!logger_properties->getString(appender_key, appender_type)) {
appender_type = "stderr";
}
- std::transform(appender_type.begin(), appender_type.end(),
appender_type.begin(), ::tolower);
+ ranges::transform(appender_type, appender_type.begin(), ::tolower);
if ("nullappender" == appender_type || "null appender" == appender_type ||
"null" == appender_type) {
sink_map[appender_name] =
std::make_shared<spdlog::sinks::null_sink_st>();
@@ -234,8 +226,7 @@ std::shared_ptr<internal::LoggerNamespace>
LoggerConfiguration::initialize_names
std::string level_name = utils::string::trim(segment);
if (first) {
first = false;
- auto opt_level = utils::parse_log_level(level_name);
- if (opt_level) {
+ if (auto opt_level = utils::parse_log_level(level_name)) {
level = *opt_level;
}
} else {
@@ -267,52 +258,58 @@ std::shared_ptr<internal::LoggerNamespace>
LoggerConfiguration::initialize_names
return root_namespace;
}
-std::shared_ptr<spdlog::logger> LoggerConfiguration::get_logger(const
std::shared_ptr<Logger>& logger, const
std::shared_ptr<internal::LoggerNamespace> &root_namespace, std::string_view
name_view,
- const
std::shared_ptr<spdlog::formatter>& formatter, bool remove_if_present) {
- std::string name{name_view};
- std::shared_ptr<spdlog::logger> spdlogger = spdlog::get(name);
- if (spdlogger) {
- if (remove_if_present) {
- spdlog::drop(name);
- } else {
- return spdlogger;
- }
+std::shared_ptr<spdlog::logger> LoggerConfiguration::get_logger(const
std::lock_guard<std::mutex>& lock,
+ const std::shared_ptr<internal::LoggerNamespace> &root_namespace,
+ const std::string& name,
+ const std::shared_ptr<spdlog::formatter>& formatter) {
+ if (auto spdlogger = spdlog::get(name)) {
+ return spdlogger;
}
+ return create_logger(lock, root_namespace, name, formatter);
+}
+
+void LoggerConfiguration::setupSpdLogger(const std::lock_guard<std::mutex>&,
+ const std::shared_ptr<spdlog::logger>& spd_logger,
+ const std::shared_ptr<internal::LoggerNamespace>& root_namespace,
+ const std::string& name,
+ const std::shared_ptr<spdlog::formatter>& formatter) {
+ if (!spd_logger)
+ return;
std::shared_ptr<internal::LoggerNamespace> current_namespace =
root_namespace;
std::vector<std::shared_ptr<spdlog::sinks::sink>> sinks =
root_namespace->sinks;
std::vector<std::shared_ptr<spdlog::sinks::sink>> inherited_sinks;
spdlog::level::level_enum level = root_namespace->level;
std::string current_namespace_str;
- std::string sink_namespace_str = "root";
- std::string level_namespace_str = "root";
for (auto const & name_segment : utils::string::split(name, "::")) {
current_namespace_str += name_segment;
auto child_pair = current_namespace->children.find(name_segment);
if (child_pair == current_namespace->children.end()) {
break;
}
- std::copy(current_namespace->exported_sinks.begin(),
current_namespace->exported_sinks.end(), std::back_inserter(inherited_sinks));
+ ranges::copy(current_namespace->exported_sinks,
std::back_inserter(inherited_sinks));
+
current_namespace = child_pair->second;
if (!current_namespace->sinks.empty()) {
sinks = current_namespace->sinks;
- sink_namespace_str = current_namespace_str;
}
if (current_namespace->has_level) {
level = current_namespace->level;
- level_namespace_str = current_namespace_str;
}
current_namespace_str += "::";
}
- if (logger != nullptr) {
- logger->log_debug("{} logger got sinks from namespace {} and level {} from
namespace {}", name, sink_namespace_str, spdlog::level::to_string_view(level),
level_namespace_str);
- }
- std::copy(inherited_sinks.begin(), inherited_sinks.end(),
std::back_inserter(sinks));
- spdlogger = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));
- spdlogger->set_level(level);
- spdlogger->set_formatter(formatter->clone());
- spdlogger->flush_on(std::max(spdlog::level::info, current_namespace->level));
+ ranges::copy(inherited_sinks, std::back_inserter(sinks));
+ spd_logger->sinks() = sinks;
+ spd_logger->set_level(level);
+ spd_logger->set_formatter(formatter->clone());
+ spd_logger->flush_on(std::max(spdlog::level::info,
current_namespace->level));
+}
+
+std::shared_ptr<spdlog::logger> LoggerConfiguration::create_logger(const
std::lock_guard<std::mutex>& lock, const
std::shared_ptr<internal::LoggerNamespace>& root_namespace, const std::string&
name,
+ const std::shared_ptr<spdlog::formatter>& formatter) {
+ const auto spd_logger =
gsl::make_not_null(std::make_shared<spdlog::logger>(name));
+ setupSpdLogger(lock, spd_logger, root_namespace, name, formatter);
try {
- spdlog::register_logger(spdlogger);
+ spdlog::register_logger(spd_logger);
} catch (const spdlog::spdlog_ex &) {
// Ignore as someone else beat us to registration, we should get the one
they made below
}
@@ -343,15 +340,14 @@ std::shared_ptr<internal::LoggerNamespace>
LoggerConfiguration::create_default_r
}
void LoggerConfiguration::initializeCompression(const
std::lock_guard<std::mutex>& lock, const std::shared_ptr<LoggerProperties>&
properties) {
- auto compression_sink = compression_manager_.initialize(properties, logger_,
[&] (const std::string& name) {return getLogger(name, std::nullopt, lock);});
- if (compression_sink) {
+ if (const auto compression_sink =
compression_manager_.initialize(properties, logger_, [&] (const std::string&
name) {return getLogger(name, std::nullopt, lock);})) {
root_namespace_->sinks.push_back(compression_sink);
root_namespace_->exported_sinks.push_back(compression_sink);
}
}
void
LoggerConfiguration::initializeAlertSinks(core::controller::ControllerServiceProvider*
controller, const std::shared_ptr<AgentIdentificationProvider>& agent_id) {
- std::lock_guard guard(mutex);
+ std::lock_guard guard(mutex_);
for (auto& sink : alert_sinks_) {
sink->initialize(controller, agent_id);
}
@@ -397,8 +393,7 @@ std::shared_ptr<spdlog::sinks::rotating_file_sink_mt>
LoggerConfiguration::getRo
}
std::lock_guard<std::mutex> guard(sink_map_mtx);
- auto it = rotating_file_sinks.find(file_name);
- if (it != rotating_file_sinks.end()) {
+ if (const auto it = rotating_file_sinks.find(file_name); it !=
rotating_file_sinks.end()) {
return it->second;
}
auto sink =
std::make_shared<spdlog::sinks::rotating_file_sink_mt>(file_name.string(),
max_file_size, max_files);
diff --git a/libminifi/test/TestBase.cpp b/libminifi/test/TestBase.cpp
index ff244ba98..bc83785c6 100644
--- a/libminifi/test/TestBase.cpp
+++ b/libminifi/test/TestBase.cpp
@@ -33,7 +33,6 @@
#include "unit/ProvenanceTestHelper.h"
#include "utils/ClassUtils.h"
#include "utils/IntegrationTestUtils.h"
-#include "core/extension/ExtensionManager.h"
#include "utils/Id.h"
#include "utils/StringUtils.h"
#include "utils/span.h"
@@ -67,7 +66,9 @@ void LogTestController::setLevel(std::string_view name,
spdlog::level::level_enu
if (config && config->shortenClassNames()) {
minifi::utils::ClassUtils::shortenClassName(adjusted_name, adjusted_name);
}
-
logging::LoggerConfiguration::getSpdlogLogger(adjusted_name)->set_level(level);
+ if (const auto spd_logger =
logging::LoggerConfiguration::getSpdlogLogger(adjusted_name)) {
+ spd_logger->set_level(level);
+ }
}
std::shared_ptr<logging::Logger>
LogTestController::getLoggerByClassName(std::string_view class_name, const
std::optional<utils::Identifier>& id) {
@@ -91,7 +92,7 @@ void
LogTestController::setLevelByClassName(spdlog::level::level_enum level, std
}
}
-bool LogTestController::contains(const std::ostringstream& stream, const
std::string& ending, std::chrono::milliseconds timeout,
std::chrono::milliseconds sleep_interval) const {
+bool LogTestController::contains(const std::ostringstream& stream, const
std::string& ending, std::chrono::milliseconds timeout,
std::chrono::milliseconds sleep_interval) {
return contains([&stream](){ return stream.str(); }, ending, timeout,
sleep_interval);
}
@@ -99,7 +100,7 @@ bool LogTestController::contains(const std::string& ending,
std::chrono::millise
return contains([this](){ return getLogs(); }, ending, timeout,
sleep_interval);
}
-bool LogTestController::contains(const std::function<std::string()>&
log_string_getter, const std::string& ending, std::chrono::milliseconds
timeout, std::chrono::milliseconds sleep_interval) const {
+bool LogTestController::contains(const std::function<std::string()>&
log_string_getter, const std::string& ending, std::chrono::milliseconds
timeout, std::chrono::milliseconds sleep_interval) {
if (ending.length() == 0) {
return false;
}
@@ -116,7 +117,6 @@ bool LogTestController::contains(const
std::function<std::string()>& log_string_
}
} while (!found && !timed_out);
- logger_->log_info("{} {} in log output.", found ? "Successfully found" :
"Failed to find", ending);
return found;
}
diff --git a/libminifi/test/TestBase.h b/libminifi/test/TestBase.h
index 27577d7ac..fed085b07 100644
--- a/libminifi/test/TestBase.h
+++ b/libminifi/test/TestBase.h
@@ -130,9 +130,9 @@ class LogTestController {
bool contains(const std::string &ending, std::chrono::milliseconds timeout =
std::chrono::seconds(3), std::chrono::milliseconds sleep_interval =
std::chrono::milliseconds(200)) const;
- bool contains(const std::ostringstream &stream, const std::string &ending,
- std::chrono::milliseconds timeout = std::chrono::seconds(3),
- std::chrono::milliseconds sleep_interval =
std::chrono::milliseconds(200)) const;
+ static bool contains(const std::ostringstream &stream, const std::string
&ending,
+ std::chrono::milliseconds timeout = std::chrono::seconds(3),
+ std::chrono::milliseconds sleep_interval =
std::chrono::milliseconds(200));
std::optional<std::smatch> matchesRegex(const std::string ®ex_str,
std::chrono::milliseconds timeout = std::chrono::seconds(3),
@@ -163,7 +163,7 @@ class LogTestController {
void init(const std::shared_ptr<logging::LoggerProperties>& logger_props);
void setLevel(std::string_view name, spdlog::level::level_enum level);
- bool contains(const std::function<std::string()>& log_string_getter, const
std::string& ending, std::chrono::milliseconds timeout,
std::chrono::milliseconds sleep_interval) const;
+ static bool contains(const std::function<std::string()>& log_string_getter,
const std::string& ending, std::chrono::milliseconds timeout,
std::chrono::milliseconds sleep_interval);
mutable std::shared_ptr<std::mutex> log_output_mutex_ =
std::make_shared<std::mutex>();
std::shared_ptr<std::ostringstream> log_output_ptr_ =
std::make_shared<std::ostringstream>();
diff --git a/libminifi/test/unit/LoggerConfigurationTests.cpp
b/libminifi/test/unit/LoggerConfigurationTests.cpp
index 8ff0e79db..7244e6156 100644
--- a/libminifi/test/unit/LoggerConfigurationTests.cpp
+++ b/libminifi/test/unit/LoggerConfigurationTests.cpp
@@ -17,16 +17,17 @@
*/
#include <algorithm>
+#include <iostream>
#include <memory>
+#include <sstream>
#include <string>
#include <vector>
-#include <sstream>
-#include <iostream>
-#include "../TestBase.h"
#include "../Catch.h"
+#include "../TestBase.h"
#include "core/logging/LoggerConfiguration.h"
#include "spdlog/formatter.h"
+#include "spdlog/pattern_formatter.h"
#include "spdlog/sinks/ostream_sink.h"
TEST_CASE("TestLoggerProperties::get_keys_of_type", "[test get_keys_of_type]")
{
@@ -42,63 +43,3 @@ TEST_CASE("TestLoggerProperties::get_keys_of_type", "[test
get_keys_of_type]") {
std::sort(actual.begin(), actual.end());
REQUIRE(expected == actual);
}
-
-#ifndef WIN32
-class TestLoggerConfiguration : public logging::LoggerConfiguration {
- public:
- static std::shared_ptr<logging::internal::LoggerNamespace>
initialize_namespaces(const std::shared_ptr<logging::LoggerProperties>
&logger_properties) {
- return
logging::LoggerConfiguration::initialize_namespaces(logger_properties);
- }
- static std::shared_ptr<spdlog::logger> get_logger(const
std::shared_ptr<logging::internal::LoggerNamespace> &root_namespace, const
std::string &name, std::shared_ptr<spdlog::formatter> formatter) {
- return
logging::LoggerConfiguration::get_logger(LogTestController::getInstance().logger_,
root_namespace, name, formatter);
- }
-};
-
-TEST_CASE("TestLoggerConfiguration::initialize_namespaces", "[test
initialize_namespaces]") {
- TestController test_controller;
- LogTestController &logTestController = LogTestController::getInstance();
- LogTestController::getInstance().setDebug<logging::LoggerProperties>();
- std::shared_ptr<logging::LoggerProperties> logger_properties =
std::make_shared<logging::LoggerProperties>();
-
- std::ostringstream stdout;
- std::ostringstream stderr;
- logger_properties->add_sink("stdout",
std::make_shared<spdlog::sinks::ostream_sink_mt>(stdout, true));
- logger_properties->add_sink("stderr",
std::make_shared<spdlog::sinks::ostream_sink_mt>(stderr, true));
-
- std::string stdout_only_warn_class =
"org::apache::nifi::minifi::fake::test::StdoutOnlyWarn";
- std::string stderr_only_error_pkg = "org::apache::nifi::minifi::fake2";
- std::string stderr_only_error_class = stderr_only_error_pkg +
"::test::StderrOnlyError";
- logger_properties->set("logger.root", "INFO,stdout,stderr");
- logger_properties->set("logger." + stdout_only_warn_class, "WARN,stdout");
- logger_properties->set("logger." + stderr_only_error_pkg, "ERROR,stderr");
-
- std::shared_ptr<logging::internal::LoggerNamespace> root_namespace =
TestLoggerConfiguration::initialize_namespaces(logger_properties);
-
- std::shared_ptr<spdlog::formatter> formatter =
std::make_shared<spdlog::pattern_formatter>(logging::LoggerConfiguration::spdlog_default_pattern);
- std::shared_ptr<spdlog::logger> logger =
TestLoggerConfiguration::get_logger(root_namespace,
"org::apache::nifi::minifi::fake::test::ClassName1", formatter);
- std::string test_log_statement = "Test log statement";
- logger->info(test_log_statement);
- REQUIRE(true == logTestController.contains(stdout, test_log_statement));
- REQUIRE(true == logTestController.contains(stderr, test_log_statement));
- logTestController.resetStream(stdout);
- logTestController.resetStream(stderr);
-
- logger = TestLoggerConfiguration::get_logger(root_namespace,
stdout_only_warn_class, formatter);
- logger->info(test_log_statement);
- REQUIRE(false == logTestController.contains(stdout, test_log_statement,
std::chrono::seconds(0)));
- logger->warn(test_log_statement);
- REQUIRE(true == logTestController.contains(stdout, test_log_statement));
- REQUIRE(false == logTestController.contains(stderr, test_log_statement,
std::chrono::seconds(0)));
- logTestController.resetStream(stdout);
- logTestController.resetStream(stderr);
-
- logger = TestLoggerConfiguration::get_logger(root_namespace,
stderr_only_error_class, formatter);
- logger->warn(test_log_statement);
- REQUIRE(false == logTestController.contains(stderr, test_log_statement,
std::chrono::seconds(0)));
- logger->error(test_log_statement);
- REQUIRE(false == logTestController.contains(stdout, test_log_statement,
std::chrono::seconds(0)));
- REQUIRE(true == logTestController.contains(stderr, test_log_statement));
- logTestController.resetStream(stdout);
- logTestController.resetStream(stderr);
-}
-#endif
diff --git a/libminifi/test/unit/LoggerTests.cpp
b/libminifi/test/unit/LoggerTests.cpp
index beb06c706..d4f45fb5d 100644
--- a/libminifi/test/unit/LoggerTests.cpp
+++ b/libminifi/test/unit/LoggerTests.cpp
@@ -30,8 +30,6 @@
#include "utils/span.h"
#include "utils/net/AsioSocketUtils.h"
-#include "spdlog/spdlog.h"
-
using namespace std::literals::chrono_literals;
TEST_CASE("Test log Levels", "[ttl1]") {
@@ -101,7 +99,6 @@ TEST_CASE("Logger configured with an ID prints this ID in
every log line", "[log
}
TEST_CASE("Printing of the ID can be disabled in the config",
"[logger][id][configuration]") {
- LogTestController::getInstance().clear();
auto properties = std::make_shared<logging::LoggerProperties>();
bool id_is_present{};
@@ -120,10 +117,8 @@ 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>(uuid);
logger->log_error("hello {}", "world");
-
-
CHECK(LogTestController::getInstance().contains("[org::apache::nifi::minifi::core::logging::Logger]
[error] hello world"));
- CHECK(id_is_present ==
LogTestController::getInstance().contains(uuid.to_string()));
- LogTestController::getInstance().reset();
+
CHECK(LogTestController::getInstance(properties)->contains("[org::apache::nifi::minifi::core::logging::Logger]
[error] hello world"));
+ CHECK(id_is_present ==
LogTestController::getInstance(properties)->contains(uuid.to_string()));
}
struct CStringConvertible {
@@ -135,6 +130,7 @@ struct CStringConvertible {
};
TEST_CASE("Test log custom string formatting", "[ttl6]") {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().clear();
LogTestController::getInstance().setTrace<logging::Logger>();
std::shared_ptr<logging::Logger> logger =
logging::LoggerFactory<logging::Logger>::getLogger();
@@ -145,6 +141,7 @@ TEST_CASE("Test log custom string formatting", "[ttl6]") {
}
TEST_CASE("Test log lazy string generation", "[ttl7]") {
+ LogTestController::getInstance().reset();
LogTestController::getInstance().clear();
LogTestController::getInstance().setDebug<logging::Logger>();
std::shared_ptr<logging::Logger> logger =
logging::LoggerFactory<logging::Logger>::getLogger();
diff --git a/minifi_main/MiNiFiMain.cpp b/minifi_main/MiNiFiMain.cpp
index 2480e53dd..1e34a09e3 100644
--- a/minifi_main/MiNiFiMain.cpp
+++ b/minifi_main/MiNiFiMain.cpp
@@ -46,7 +46,6 @@
#include <atomic>
#include <cstdlib>
#include <iostream>
-#include <memory>
#include <vector>
#include "ResourceClaim.h"
@@ -69,7 +68,6 @@
#include "agent/JsonSchema.h"
#include "core/state/nodes/ResponseNodeLoader.h"
#include "c2/C2Agent.h"
-#include "core/state/MetricsPublisherFactory.h"
#include "core/state/MetricsPublisherStore.h"
#include "argparse/argparse.hpp"
#include "agent/agent_version.h"
@@ -116,8 +114,8 @@ void sigHandler(int signal) {
}
}
-void dumpDocs(const std::shared_ptr<minifi::Configure> &configuration, const
std::string &dir) {
- auto pythoncreator =
core::ClassLoader::getDefaultClassLoader().instantiate("PythonCreator",
"PythonCreator");
+void dumpDocs(const std::shared_ptr<minifi::Configure>& configuration, const
std::string& dir) {
+ const auto pythoncreator =
core::ClassLoader::getDefaultClassLoader().instantiate("PythonCreator",
"PythonCreator");
if (nullptr != pythoncreator) {
pythoncreator->configure(configuration);
}
@@ -127,8 +125,8 @@ void dumpDocs(const std::shared_ptr<minifi::Configure>
&configuration, const std
docsCreator.generate(dir);
}
-void writeJsonSchema(const std::shared_ptr<minifi::Configure> &configuration,
std::ostream& out) {
- auto pythoncreator =
core::ClassLoader::getDefaultClassLoader().instantiate("PythonCreator",
"PythonCreator");
+void writeJsonSchema(const std::shared_ptr<minifi::Configure>& configuration,
std::ostream& out) {
+ const auto pythoncreator =
core::ClassLoader::getDefaultClassLoader().instantiate("PythonCreator",
"PythonCreator");
if (nullptr != pythoncreator) {
pythoncreator->configure(configuration);
}
@@ -174,7 +172,7 @@ void writeSchemaIfRequested(const argparse::ArgumentParser&
parser, const std::s
std::exit(1);
}
- auto parent_dir = std::filesystem::path(schema_path).parent_path();
+ const auto parent_dir = std::filesystem::path(schema_path).parent_path();
if (utils::file::create_dir(parent_dir) != 0) {
std::cerr << "JSON schema parent directory doesn't exist and cannot be
created: " << parent_dir << std::endl;
std::exit(1);
@@ -224,8 +222,10 @@ int main(int argc, char **argv) {
if (utils::Environment::isRunningAsService()) {
setSyslogLogger();
}
- const auto logger =
core::logging::LoggerConfiguration::getConfiguration().getLogger("main");
-
+ auto& logger_configuration =
core::logging::LoggerConfiguration::getConfiguration();
+ const auto logger = logger_configuration.getLogger("main");
+ auto startup_timepoint = std::chrono::system_clock::now();
+ auto log_runtime = gsl::finally([&](){logger->log_info("Runtime was {}",
std::chrono::system_clock::now()-startup_timepoint);});
#ifdef WIN32
if (isStartedByService) {
if (!CreateServiceTerminationThread(logger, terminationEventHandler)) {
@@ -311,7 +311,7 @@ int main(int argc, char **argv) {
log_properties->setHome(minifiHome);
log_properties->loadConfigureFile(DEFAULT_LOG_PROPERTIES_FILE,
"nifi.log.");
-
core::logging::LoggerConfiguration::getConfiguration().initialize(log_properties);
+ logger_configuration.initialize(log_properties);
std::shared_ptr<minifi::Properties> uid_properties =
std::make_shared<minifi::Properties>("UID properties");
uid_properties->setHome(minifiHome);
@@ -404,7 +404,7 @@ int main(int argc, char **argv) {
auto metrics_publisher_store =
std::make_unique<minifi::state::MetricsPublisherStore>(configure,
repo_metric_sources, flow_configuration);
const auto controller = std::make_unique<minifi::FlowController>(
- prov_repo, flow_repo, configure, std::move(flow_configuration),
content_repo, std::move(metrics_publisher_store), filesystem, request_restart);
+ prov_repo, flow_repo, configure, std::move(flow_configuration),
content_repo, std::move(metrics_publisher_store), filesystem, request_restart);
const bool disk_space_watchdog_enable =
configure->get(minifi::Configure::minifi_disk_space_watchdog_enable)
| utils::andThen(utils::string::toBool)
@@ -465,12 +465,10 @@ int main(int argc, char **argv) {
// Load flow from specified configuration file
try {
controller->load();
- }
- catch (std::exception& e) {
+ } catch (std::exception& e) {
logger->log_error("Failed to load configuration due to exception: {}",
e.what());
return -1;
- }
- catch (...) {
+ } catch (...) {
logger->log_error("Failed to load configuration due to unknown
exception");
return -1;
}