diff --git a/include/xrpl/basics/Log.h b/include/xrpl/basics/Log.h index 2c407dda17..8df19045a1 100644 --- a/include/xrpl/basics/Log.h +++ b/include/xrpl/basics/Log.h @@ -256,19 +256,19 @@ public: static LogSeverity fromString(std::string const& s); -private: - enum { - // Maximum line length for log messages. - // If the message exceeds this length it will be truncated with elipses. - maximumMessageCharacters = 12 * 1024 - }; - static void format( std::string& output, std::string const& message, beast::severities::Severity severity, std::string const& partition); + +private: + enum { + // Maximum line length for log messages. + // If the message exceeds this length it will be truncated with elipses. + maximumMessageCharacters = 12 * 1024 + }; }; // Wraps a Journal::Stream to skip evaluation of diff --git a/include/xrpl/beast/utility/Journal.h b/include/xrpl/beast/utility/Journal.h index c02bdb88f9..414618016c 100644 --- a/include/xrpl/beast/utility/Journal.h +++ b/include/xrpl/beast/utility/Journal.h @@ -90,6 +90,12 @@ public: m_structuredJournalImpl = impl; } + static void + disableStructuredJournal() + { + m_structuredJournalImpl = nullptr; + } + static bool isStructuredJournalEnabled() { diff --git a/src/tests/libxrpl/basics/log.cpp b/src/tests/libxrpl/basics/log.cpp index 7c3710bfe6..108eb146cd 100644 --- a/src/tests/libxrpl/basics/log.cpp +++ b/src/tests/libxrpl/basics/log.cpp @@ -81,7 +81,46 @@ public: } }; -TEST_CASE("Enable Json Logs") +TEST_CASE("Text logs") +{ + std::stringstream logStream; + + MockLogs logs{logStream, beast::severities::kAll}; + + logs.journal("Test").debug() << "Test"; + + CHECK(logStream.str().find("Test") != std::string::npos); + + logStream.str(""); + + logs.journal("Test").debug() << "\n"; + + CHECK(logStream.str().find("\n") == std::string::npos); +} + + +TEST_CASE("Test format output") +{ + std::string output; + Logs::format(output, "Message", beast::severities::kDebug, "Test"); + CHECK(output.find("Message") != std::string::npos); + CHECK(output != "Message"); +} + +TEST_CASE("Test format output when structured logs are enabled") +{ + static log::JsonStructuredJournal structuredJournal; + beast::Journal::enableStructuredJournal(&structuredJournal); + + std::string output; + Logs::format(output, "Message", beast::severities::kDebug, "Test"); + + CHECK(output == "Message"); + + beast::Journal::disableStructuredJournal(); +} + +TEST_CASE("Enable json logs") { static log::JsonStructuredJournal structuredJournal; @@ -97,7 +136,7 @@ TEST_CASE("Enable Json Logs") beast::Journal::enableStructuredJournal(&structuredJournal); - logs.journal("Test").debug() << "Test"; + logs.journal("Test").debug() << "\n"; Json::Reader reader; Json::Value jsonLog; @@ -108,7 +147,8 @@ TEST_CASE("Enable Json Logs") CHECK(jsonLog.isObject()); CHECK(jsonLog.isMember("Message")); CHECK(jsonLog["Message"].isString()); - CHECK(jsonLog["Message"].asString() == "Test"); + CHECK(jsonLog["Message"].asString() == ""); + beast::Journal::disableStructuredJournal(); } TEST_CASE("Global attributes") @@ -134,6 +174,7 @@ TEST_CASE("Global attributes") CHECK(jsonLog.isMember("Field1")); CHECK(jsonLog["Field1"].isString()); CHECK(jsonLog["Field1"].asString() == "Value1"); + beast::Journal::disableStructuredJournal(); } TEST_CASE("Global attributes inheritable") @@ -166,4 +207,5 @@ TEST_CASE("Global attributes inheritable") CHECK(jsonLog["Field1"].asString() == "Value3"); CHECK(jsonLog["Field2"].isString()); CHECK(jsonLog["Field2"].asString() == "Value2"); + beast::Journal::disableStructuredJournal(); } \ No newline at end of file diff --git a/src/tests/libxrpl/telemetry/json_logs.cpp b/src/tests/libxrpl/telemetry/json_logs.cpp index 209606dc8c..ac8a80af6d 100644 --- a/src/tests/libxrpl/telemetry/json_logs.cpp +++ b/src/tests/libxrpl/telemetry/json_logs.cpp @@ -62,6 +62,11 @@ public: beast::Journal::enableStructuredJournal(&structuredJournal); } + ~JsonLogStreamFixture() + { + beast::Journal::disableStructuredJournal(); + } + std::stringstream& stream() { @@ -82,7 +87,12 @@ private: TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields") { - journal().debug() << "Test"; + journal().debug() << std::boolalpha + << true + << std::noboolalpha + << " Test " + << std::boolalpha + << false; Json::Value logValue; Json::Reader reader; @@ -103,7 +113,7 @@ TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields") CHECK(logValue["Line"].isNumeric()); CHECK(logValue["Params"].isNull()); CHECK(logValue["Message"].isString()); - CHECK(logValue["Message"].asString() == "Test"); + CHECK(logValue["Message"].asString() == "true Test false"); } TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogLevels")