Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2017-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test debug logging."""7import os9from test_framework.test_framework import BitcoinTestFramework10from test_framework.p2p import P2PInterface11from test_framework.test_node import ErrorMatch14class LoggingTest(BitcoinTestFramework):15def set_test_params(self):16self.num_nodes = 117self.setup_clean_chain = True19def relative_log_path(self, name):20return os.path.join(self.nodes[0].chain_path, name)22def run_test(self):23# test default log file name24default_log_path = self.relative_log_path("debug.log")25assert os.path.isfile(default_log_path)27# test alternative log file name in datadir28self.restart_node(0, ["-debuglogfile=foo.log"])29assert os.path.isfile(self.relative_log_path("foo.log"))31# test alternative log file name outside datadir32tempname = os.path.join(self.options.tmpdir, "foo.log")33self.restart_node(0, [f"-debuglogfile={tempname}"])34assert os.path.isfile(tempname)36# check that invalid log (relative) will cause error37invdir = self.relative_log_path("foo")38invalidname = os.path.join("foo", "foo.log")39self.stop_node(0)40exp_stderr = "Error: Could not open debug log file "41self.nodes[0].assert_start_raises_init_error([f"-debuglogfile={invalidname}"], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)42assert not os.path.isfile(os.path.join(invdir, "foo.log"))44# check that invalid log (relative) works after path exists45self.stop_node(0)46os.mkdir(invdir)47self.start_node(0, [f"-debuglogfile={invalidname}"])48assert os.path.isfile(os.path.join(invdir, "foo.log"))50# check that invalid log (absolute) will cause error51self.stop_node(0)52invdir = os.path.join(self.options.tmpdir, "foo")53invalidname = os.path.join(invdir, "foo.log")54self.nodes[0].assert_start_raises_init_error([f"-debuglogfile={invalidname}"], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)55assert not os.path.isfile(os.path.join(invdir, "foo.log"))57# check that invalid log (absolute) works after path exists58self.stop_node(0)59os.mkdir(invdir)60self.start_node(0, [f"-debuglogfile={invalidname}"])61assert os.path.isfile(os.path.join(invdir, "foo.log"))63# check that -nodebuglogfile disables logging64self.stop_node(0)65os.unlink(default_log_path)66assert not os.path.isfile(default_log_path)67self.start_node(0, ["-nodebuglogfile"])68assert not os.path.isfile(default_log_path)70# just sanity check no crash here71self.restart_node(0, [f"-debuglogfile={os.devnull}"])73self.log.info("Test -debug and -debugexclude raise when invalid values are passed")74self.stop_node(0)75self.nodes[0].assert_start_raises_init_error(76extra_args=["-debug=abc"],77expected_msg="Error: Unsupported logging category -debug=abc.",78match=ErrorMatch.FULL_REGEX,79)80self.nodes[0].assert_start_raises_init_error(81extra_args=["-debugexclude=abc"],82expected_msg="Error: Unsupported logging category -debugexclude=abc.",83match=ErrorMatch.FULL_REGEX,84)86self.log.info("Test -loglevel raises when invalid values are passed")87self.nodes[0].assert_start_raises_init_error(88extra_args=["-loglevel=abc"],89expected_msg="Error: Unsupported global logging level -loglevel=abc. Valid values: info, debug, trace.",90match=ErrorMatch.FULL_REGEX,91)92self.nodes[0].assert_start_raises_init_error(93extra_args=["-loglevel=net:abc"],94expected_msg="Error: Unsupported category-specific logging level -loglevel=net:abc.",95match=ErrorMatch.PARTIAL_REGEX,96)97self.nodes[0].assert_start_raises_init_error(98extra_args=["-loglevel=net:info:abc"],99expected_msg="Error: Unsupported category-specific logging level -loglevel=net:info:abc.",100match=ErrorMatch.PARTIAL_REGEX,101)103self.log.info("Test that -nodebug,-debug=0,-debug=none clear previously specified debug options")104disable_debug_options = [105'-debug=0',106'-debug=none',107'-nodebug'108]110for disable_debug_opt in disable_debug_options:111# Every category before disable_debug_opt will be ignored, including the invalid 'abc'112self.restart_node(0, ['-debug=http', '-debug=abc', disable_debug_opt, '-debug=rpc', '-debug=net'])113logging = self.nodes[0].logging()114assert not logging['http']115assert 'abc' not in logging116assert logging['rpc']117assert logging['net']119self.log.info("Test -logips formatting in net logs")120self.restart_node(0, ['-debug=net', '-logips=1'])121with self.nodes[0].assert_debug_log(["peer=0, peeraddr="]):122p2p = self.nodes[0].add_p2p_connection(P2PInterface())123p2p.wait_for_verack()124self.nodes[0].disconnect_p2ps()126if __name__ == '__main__':127LoggingTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.