Create BCLog::Level::Trace log severity level

for verbose log messages for development or debugging only, as bitcoind may run
more slowly, that are more granular/frequent than the Debug log level, i.e. for
very high-frequency, low-level messages to be logged distinctly from
higher-level, less-frequent debug logging that could still be usable in production.

An example would be to log higher-level peer events (connection, disconnection,
misbehavior, eviction) as Debug, versus Trace for low-level, high-volume p2p
messages in the BCLog::NET category. This will enable the user to log only the
former without the latter, in order to focus on high-level peer management events.

With respect to the name, "trace" is suggested as the most granular level
in resources like the following:
- https://sematext.com/blog/logging-levels
- https://howtodoinjava.com/log4j2/logging-levels

Update the test framework and add test coverage.
This commit is contained in:
Jon Atack committed 2022-08-20 11:55:17 +02:00
1 parent 2a8712db4f
commit 45f9282162
6 files changed
+19 -14

No files matched your search

+7 -3
View File
@@ -202,6 +202,8 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
{ {
switch (level) { switch (level) {
case BCLog::Level::Trace:
return "trace";
case BCLog::Level::Debug: case BCLog::Level::Debug:
return "debug"; return "debug";
case BCLog::Level::Info: case BCLog::Level::Info:
@@ -286,7 +288,9 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str) static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str)
{ {
if (level_str == "debug") { if (level_str == "trace") {
return BCLog::Level::Trace;
} else if (level_str == "debug") {
return BCLog::Level::Debug; return BCLog::Level::Debug;
} else if (level_str == "info") { } else if (level_str == "info") {
return BCLog::Level::Info; return BCLog::Level::Info;
@@ -320,9 +324,9 @@ std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
} }
/** Log severity levels that can be selected by the user. */ /** Log severity levels that can be selected by the user. */
static constexpr std::array<BCLog::Level, 2> LogLevelsList() static constexpr std::array<BCLog::Level, 3> LogLevelsList()
{ {
return {BCLog::Level::Info, BCLog::Level::Debug}; return {BCLog::Level::Info, BCLog::Level::Debug, BCLog::Level::Trace};
} }
std::string BCLog::Logger::LogLevelsString() const std::string BCLog::Logger::LogLevelsString() const
+2 -1
View File
@@ -69,7 +69,8 @@ namespace BCLog {
ALL = ~(uint32_t)0, ALL = ~(uint32_t)0,
}; };
enum class Level { enum class Level {
Debug = 0, // High-volume or detailed logging for development/debugging Trace = 0, // High-volume or detailed logging for development/debugging
Debug, // Reasonably noisy logging, but still usable in production
Info, // Default Info, // Default
Warning, Warning,
Error, Error,
+1 -1
View File
@@ -21,7 +21,7 @@ BOOST_FIXTURE_TEST_SUITE(i2p_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(unlimited_recv) BOOST_AUTO_TEST_CASE(unlimited_recv)
{ {
const auto prev_log_level{LogInstance().LogLevel()}; const auto prev_log_level{LogInstance().LogLevel()};
LogInstance().SetLogLevel(BCLog::Level::Debug); LogInstance().SetLogLevel(BCLog::Level::Trace);
auto CreateSockOrig = CreateSock; auto CreateSockOrig = CreateSock;
// Mock CreateSock() to create MockSock. // Mock CreateSock() to create MockSock.
+7 -7
View File
@@ -172,18 +172,18 @@ BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
{ {
LogInstance().EnableCategory(BCLog::LogFlags::ALL); LogInstance().EnableCategory(BCLog::LogFlags::ALL);
LogInstance().SetLogLevel(BCLog::Level::Info); LogInstance().SetLogLevel(BCLog::Level::Debug);
LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info"); LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
// Global log level // Global log level
LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1"); LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1");
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Debug, "foo2: %s. This log level is lower than the global one.\n", "bar2"); LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Trace, "foo2: %s. This log level is lower than the global one.\n", "bar2");
LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Warning, "foo3: %s\n", "bar3"); LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Warning, "foo3: %s\n", "bar3");
LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4"); LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4");
// Category-specific log level // Category-specific log level
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5"); LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5");
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo6: %s. This log level is lower than the category-specific one.\n", "bar6"); LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo6: %s. This log level is the same as the global one but lower than the category-specific one, which takes precedence. \n", "bar6");
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7"); LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7");
std::vector<std::string> expected = { std::vector<std::string> expected = {
@@ -220,7 +220,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
ResetLogger(); ResetLogger();
ArgsManager args; ArgsManager args;
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
const char* argv_test[] = {"bitcoind", "-loglevel=net:debug"}; const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
std::string err; std::string err;
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err)); BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
init::SetLoggingLevel(args); init::SetLoggingLevel(args);
@@ -229,7 +229,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
const auto& category_levels{LogInstance().CategoryLevels()}; const auto& category_levels{LogInstance().CategoryLevels()};
const auto net_it{category_levels.find(BCLog::LogFlags::NET)}; const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
BOOST_REQUIRE(net_it != category_levels.end()); BOOST_REQUIRE(net_it != category_levels.end());
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug); BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
} }
// Set both global log level and category-specific log level // Set both global log level and category-specific log level
@@ -237,7 +237,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
ResetLogger(); ResetLogger();
ArgsManager args; ArgsManager args;
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:debug", "-loglevel=http:info"}; const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
std::string err; std::string err;
BOOST_REQUIRE(args.ParseParameters(4, argv_test, err)); BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
init::SetLoggingLevel(args); init::SetLoggingLevel(args);
@@ -248,7 +248,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
const auto net_it{category_levels.find(BCLog::LogFlags::NET)}; const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
BOOST_CHECK(net_it != category_levels.end()); BOOST_CHECK(net_it != category_levels.end());
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug); BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)}; const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
BOOST_CHECK(http_it != category_levels.end()); BOOST_CHECK(http_it != category_levels.end());
+1 -1
View File
@@ -108,7 +108,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
"-logsourcelocations", "-logsourcelocations",
"-logtimemicros", "-logtimemicros",
"-logthreadnames", "-logthreadnames",
"-loglevel=debug", "-loglevel=trace",
"-debug", "-debug",
"-debugexclude=libevent", "-debugexclude=libevent",
"-debugexclude=leveldb", "-debugexclude=leveldb",
+1 -1
View File
@@ -119,7 +119,7 @@ class TestNode():
if self.version_is_at_least(219900): if self.version_is_at_least(219900):
self.args.append("-logsourcelocations") self.args.append("-logsourcelocations")
if self.version_is_at_least(239000): if self.version_is_at_least(239000):
self.args.append("-loglevel=debug") self.args.append("-loglevel=trace")
self.cli = TestNodeCLI(bitcoin_cli, self.datadir) self.cli = TestNodeCLI(bitcoin_cli, self.datadir)
self.use_cli = use_cli self.use_cli = use_cli