-
Notifications
You must be signed in to change notification settings - Fork 38.7k
log, refactor: Allow log macros to accept context arguments #29256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/29256. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible typos and grammar issues:
2025-12-16 |
|
Concept NACK from me, this seems much uglier and trickier to work with. I've already written in depth about why the current approach makes sense, so I'll be brief here.
Being able to avoid logging critical messages is adding a bug, not a feature.
"+841 -626" and adding a dummy parameter to most calls does not make things less verbose, and it's also much harder to search for net related logging when the individual log statements just say
The only thing needed here is renaming from |
|
Hi AJ, this is a draft, and there will be some more changes which may address your concerns.
Agree, but the idea here is not to discard log messages, the idea just is to attach meaningful metadata to log messages so they can be filtered by component.
I can probably make the description clearer, but the idea here is that this will make the code less noisy: -LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "getsockname failed\n");
+LogWarning(log, "getsockname failed\n");It is true this sacrifices ability to grep by category constants in the source code, and maybe in your judgement that is an unacceptable cost? But in my opinion, if we ever using same category constants in disparate parts of source code, or different category constants in the same part of the source code, it means source code is badly organized and we should fix that instead of resorting to a coping mechanism of searching for constants instead of searching by source path. In my experience, I've haven't used logging frameworks that have encouraged category constants to be scattered and mixed like this, and I don't think it is a good idea. The "+841 -626" refactoring is definitely 💩-y and I plan to drop it from this PR. The change to the log macros is meant to facilitate that refactoring, not the other way around. I do think we should stop relying on the global logging instance for libbitcoinkernel code, but probably can keep using it elsewhere.
Probably something is lost in communication here, but the idea is to let wallet code use same LogDebug(), LogTrace(), LogInfo(), LogWarning(), LogError() macros other code uses. It just adds a little formatting hook so the wallet name is automatically included when the log source is the wallet. This way full functionality of #29256 is available to the wallet and we can drop WalletLogPrintf and not have dedicated wallet logging functions. |
|
🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the Possibly this is due to a silent merge conflict (the changes in this pull request being Leave a comment here, if you need help tracking down a confusing failure. |
f929469 to
15c08bc
Compare
Filtering is discarding. If you just want to find log messages, that what's grep is for, and if you want to make it more fine-grained than "hey this is an important log message/warning/error", that's what If it's any help, I think it's better to think of the sections as not related to which section of the code they appear in so much (again, that's what
The current code there is noisy because it uses -LogWarning("getsockname failed\n");
+LogWarning(log, "getsockname failed\n");which is adding characters, without adding any information to the reader.
I think
That's something that already happens:
I don't think there is anything here "encouraging" these categories to be scattered or mixed; most of them are specific to a small set of related files: TOR:torcontrol, HTTP:httpserver, ZMQ:zmq/, WALLETDB:wallet/, ESTIMATEFEE:policy/fees.cpp, SELECTCOINS:wallet/coinselection, REINDEX:validation, CMPCTBLOCK:blockencodings, RAND:random.cpp, etc. And you obviously have worked with code designed this way: that's how
Updating
A +190 -109 diff because you don't like a one-line wrapper function seems crazy to me. Do you have some reason to think there's large amounts of additional wallet debug logs that would be introduced if it didn't involve writing |
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Test will be extended in next commit to cover support for context objects.
Allow LogDebug(), LogTrace(), LogInfo(), LogWarning(), and LogError() macros to accept context arguments to provide more information in log messages and more control over logging to callers. This functionality is used in followup PRs: - bitcoin#30342 - To let libbitcoinkernel send output to specfic `BCLog::Logger` instances instead of a global instance, so output can be disambiguated and applications can have more control over logging. - bitcoin#30343 - To replace custom `WalletLogPrintf` calls with standard logging calls that automatically include wallet names and don't log everything at info level. This commit does not change behavior of current log prints or require them to be updated. It includes tests and documentation covering the new functionality. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Custom log contexts allow overridding log formatting and adding metadata such as request ids or wallet names to log messages while still using standard macros for logging. This is used to replace WalletLogPrintf() functions with LogInfo() calls in followup PR bitcoin#30343.
Needed to fix errors like: const Context &GetContext(const Context &)': expects 1 arguments - 3 provided due to a compiler bug: https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly/5134656#5134656 Example CI failure: https://github.com/bitcoin/bitcoin/actions/runs/8072891468/job/22055528830?pr=29256
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and LogError() macros, and disallow omitting BCLog categories when calling LogDebug() and LogTrace() macros. Additionally, drop category information from log output at higher levels as suggested by Hodlinator bitcoin#29256 (comment). None of these restrictions are technically necessary, and not everybody believes they are good. However, they have existed since the Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and removing these restrictions is orthogonal to the goals of this PR which are: (1) to allow the Log macros to work without accessing global state and (2) to support local logging customization with additional metadata. This change just more clearly documents the current logging restrictions and provides better error messages to developers when enforcing them. Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
|
🐙 This pull request conflicts with the target branch and needs rebase. |
Allow
LogDebug(),LogTrace(),LogInfo(),LogWarning(), andLogError()macros to accept context arguments to provide more information in log messages and more control over logging to callers.This functionality is used in followup PRs:
#30342 - To let libbitcoinkernel send output to specfic
BCLog::Loggerinstances instead of a global instance, so output can be disambiguated and applications can have more control over logging.#30343 - To replace custom
WalletLogPrintfcalls with standard logging calls that automatically include wallet names and don't log everything at info level.This PR does not change behavior of current log prints or require them to be updated. It includes tests and documentation covering the new functionality.
Note: Originally this PR also removed some restrictions around passing category constants to log macros to try to make them more consistent, but these changes were too controversial and have been dropped.