Skip to content

Commit 45f9282

Browse files
committed
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.
1 parent 2a8712d commit 45f9282

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

src/logging.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
202202
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
203203
{
204204
switch (level) {
205+
case BCLog::Level::Trace:
206+
return "trace";
205207
case BCLog::Level::Debug:
206208
return "debug";
207209
case BCLog::Level::Info:
@@ -286,7 +288,9 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
286288

287289
static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str)
288290
{
289-
if (level_str == "debug") {
291+
if (level_str == "trace") {
292+
return BCLog::Level::Trace;
293+
} else if (level_str == "debug") {
290294
return BCLog::Level::Debug;
291295
} else if (level_str == "info") {
292296
return BCLog::Level::Info;
@@ -320,9 +324,9 @@ std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
320324
}
321325

322326
/** Log severity levels that can be selected by the user. */
323-
static constexpr std::array<BCLog::Level, 2> LogLevelsList()
327+
static constexpr std::array<BCLog::Level, 3> LogLevelsList()
324328
{
325-
return {BCLog::Level::Info, BCLog::Level::Debug};
329+
return {BCLog::Level::Info, BCLog::Level::Debug, BCLog::Level::Trace};
326330
}
327331

328332
std::string BCLog::Logger::LogLevelsString() const

src/logging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ namespace BCLog {
6969
ALL = ~(uint32_t)0,
7070
};
7171
enum class Level {
72-
Debug = 0, // High-volume or detailed logging for development/debugging
72+
Trace = 0, // High-volume or detailed logging for development/debugging
73+
Debug, // Reasonably noisy logging, but still usable in production
7374
Info, // Default
7475
Warning,
7576
Error,

src/test/i2p_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BOOST_FIXTURE_TEST_SUITE(i2p_tests, BasicTestingSetup)
2121
BOOST_AUTO_TEST_CASE(unlimited_recv)
2222
{
2323
const auto prev_log_level{LogInstance().LogLevel()};
24-
LogInstance().SetLogLevel(BCLog::Level::Debug);
24+
LogInstance().SetLogLevel(BCLog::Level::Trace);
2525
auto CreateSockOrig = CreateSock;
2626

2727
// Mock CreateSock() to create MockSock.

src/test/logging_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,18 @@ BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
172172
{
173173
LogInstance().EnableCategory(BCLog::LogFlags::ALL);
174174

175-
LogInstance().SetLogLevel(BCLog::Level::Info);
175+
LogInstance().SetLogLevel(BCLog::Level::Debug);
176176
LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
177177

178178
// Global log level
179179
LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1");
180-
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Debug, "foo2: %s. This log level is lower than the global one.\n", "bar2");
180+
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Trace, "foo2: %s. This log level is lower than the global one.\n", "bar2");
181181
LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Warning, "foo3: %s\n", "bar3");
182182
LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4");
183183

184184
// Category-specific log level
185185
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5");
186-
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo6: %s. This log level is lower than the category-specific one.\n", "bar6");
186+
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");
187187
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7");
188188

189189
std::vector<std::string> expected = {
@@ -220,7 +220,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
220220
ResetLogger();
221221
ArgsManager args;
222222
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
223-
const char* argv_test[] = {"bitcoind", "-loglevel=net:debug"};
223+
const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
224224
std::string err;
225225
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
226226
init::SetLoggingLevel(args);
@@ -229,15 +229,15 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
229229
const auto& category_levels{LogInstance().CategoryLevels()};
230230
const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
231231
BOOST_REQUIRE(net_it != category_levels.end());
232-
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug);
232+
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
233233
}
234234

235235
// Set both global log level and category-specific log level
236236
{
237237
ResetLogger();
238238
ArgsManager args;
239239
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
240-
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:debug", "-loglevel=http:info"};
240+
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
241241
std::string err;
242242
BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
243243
init::SetLoggingLevel(args);
@@ -248,7 +248,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
248248

249249
const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
250250
BOOST_CHECK(net_it != category_levels.end());
251-
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug);
251+
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
252252

253253
const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
254254
BOOST_CHECK(http_it != category_levels.end());

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
108108
"-logsourcelocations",
109109
"-logtimemicros",
110110
"-logthreadnames",
111-
"-loglevel=debug",
111+
"-loglevel=trace",
112112
"-debug",
113113
"-debugexclude=libevent",
114114
"-debugexclude=leveldb",

test/functional/test_framework/test_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitc
119119
if self.version_is_at_least(219900):
120120
self.args.append("-logsourcelocations")
121121
if self.version_is_at_least(239000):
122-
self.args.append("-loglevel=debug")
122+
self.args.append("-loglevel=trace")
123123

124124
self.cli = TestNodeCLI(bitcoin_cli, self.datadir)
125125
self.use_cli = use_cli

0 commit comments

Comments
 (0)