Repository navigation
Description
tl;dr: kernel logging is cumbersome. I propose we layer logging so we can enable structured logging and a much simplified kernel logging interface without overcomplicating node logging.
Motivation
The bitcoinkernel library (#27587) exposes functionality to interface with the kernel logging. This includes registering callbacks for log statements, level/category filtering, string formatting options, and more.
Kernel logging has a few problems:
- P1: callbacks operate on formatted strings, so users need to parse the string to get the timestamp, category, level, ... based on which options are set. This is cumbersome, brittle, and inefficient.
- P2: the filtering interface is not really intuitive, requiring users to call combinations of
btck_logging_set_level_categoryandbtck_logging_enable_categorywhen they want to producedebugortracelogs. The level/category system makes sense for node, because it directly controls what gets written to disk and stdout, and there are quite a lot more categories producing logs. Kernel doesn't really need this - users control what happens to the logs, and can do any filtering/manipulation in the callback they provide. - P3: the node logging infrastructure has quite a bit more functionality than is necessary for a library, including ratelimiting, log formatting, outputting, buffering, ... This introduces unnecessary code and interface complexity.
Approach
I propose we address problems P1, P2 and P3 by layering the logging infrastructure. Roughly, this would mean:
- Implement a minimal
util::log::Loggerclass (see Appendix) that dispatches structured log entries to registered callbacks. This is a low-level logger that does not perform formatting, rate limiting, or category filtering - it simply dispatches to callbacks. - Refactor
BCLog::LoggerintoBCLog::Sink, which registers a callback onutil::log::GetLogger()and handles all node-specific functionality (category filtering, rate limiting, file output, formatting, ...). - Update kernel code to include
util/log.hinstead oflogging.h. The existing logging macros (LogInfo,LogDebug, etc.) continue to work unchanged. - Simplify
bitcoinkernelC API to reflect the simplerutil::log::Loggerinterface (structured logging, level-based, ...), we can now completely removebtck_LoggingOptions. - Remove
logging.handlogging.cppfrom the kernel library completely.
Balance
The main benefits and downsides of this approach:
Benefits:
- B1: a kernel-native structured logging interface that minimizes complexity and makes for a much more ergonomic interface, enabling structured logging instead of string parsing.
- B2: doesn't complicate node logging purely for the purpose of accommodating diverging requirements for kernel. For example, I suspect projects like kernel, logging: Deliver each context's log output to its own logging connection #30342 should be much easier with layered kernel logging.
- B3: reduces friction if/when node migrates to use the public
bitcoinkernelinterface by having already completely decoupled logging. - ... (what have I missed?)
Downsides:
- D1: levels-based filtering has some performance overhead by formatting all (kernel) debug log strings instead of just the subset of categories specified. Note that this only applies when debug is enabled, and that kernel has a lot less logging than node.
- ... (what have I missed?)
Scope
I have implemented this approach in master...stickies-v:bitcoin:2025-12/kernel-logging-layering. Most of the changes are quite straightforward, including refactoring BCLog::Logger to BCLog::Sink, adding tests, and adding a util::log::Logger interface.
Appendix
Interfaces
util::log::Logger
class Logger
{
public:
using Callback = std::function<void(const Entry&)>;
using CallbackHandle = std::list<Callback>::iterator;
//! Register a callback to receive log entries. Returns a handle for unregistration.
[[nodiscard]] CallbackHandle RegisterCallback(Callback callback);
//! Unregister a previously registered callback.
void UnregisterCallback(CallbackHandle handle);
//! Set the minimum log level. Messages below this level are discarded.
void SetMinLevel(Level level);
//! Get the current minimum log level.
Level GetMinLevel() const;
//! Returns true if a message at the given level would be logged.
bool WillLog(Level level) const;
//! Returns true if any callbacks are registered.
bool Enabled() const;
//! Format message and dispatch to all registered callbacks. No-op if minimum log level not met.
template <typename... Args>
void Log(Level level, uint64_t category, std::source_location loc, bool should_ratelimit,
util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args);
};bitcoinkernel C logging interface
typedef void (*btck_LogCallback)(void* user_data, const btck_LogEntry* entry);
struct btck_LogEntry {
const char* message; //!< Log message
size_t message_len; //!< Log message length
const char* file_name; //!< Source file name
size_t file_name_len; //!< Source file name length
const char* function_name; //!< Source function name
size_t function_name_len; //!< Source function name length
const char* thread_name; //!< Thread name
size_t thread_name_len; //!< Thread name length
int64_t timestamp_ns; //!< Timestamp in nanoseconds since epoch
uint32_t line; //!< Source line number
btck_LogLevel level; //!< Log level
btck_LogCategory category; //!< Log category
};
BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level);
BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create(
btck_LogCallback log_callback,
void* user_data,
btck_DestroyCallback user_data_destroy_callback) BITCOINKERNEL_ARG_NONNULL(1);
BITCOINKERNEL_API void btck_logging_connection_destroy(btck_LoggingConnection* logging_connection);Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone or Closed or Rethinking
Activity
ryanofsky commentedon Dec 12, 2025
Nice work. The new interface proposed here and implemented in your branch seem much better than the current interface, and well thought out. I feel like we could pretty easily wire it into the existing Logger class without introducing a new class and new macros though, and without needing to change validation & util code.
I wonder if there may be difficulties doing that I'm not aware of, or if it just seemed easier to start with a clean state in the current branch.
I do think it would preferable to have one Logger class and one set of macros instead of two. I also think using kernel logging functions in util code (only
src/util/fs_helpers.cppright now but it seems like usage might expand) would probably not be desirable, since the dependency is supposed to to the other way with kernel depending on util, not util depending on kernel. Also, the wallet and GUI are supposed to use util but should not depend on the kernel. This could be another reason to want to try improving theLoggerclass instead of replacing it.stickies-v commentedon Dec 15, 2025
Thanks for looking into this, @ryanofsky! Upgrading vs separating is indeed the important decision to make here. I initially started wiring it into the existing
Loggerclass, but changed my approach when I realized that whenkernelbecomes its own project, they will each need to have their own code anyway. Complicatingnodelogging to accommodate kernel's different requirements, to then take it away again, feels like unnecessary friction and code complexity (e.g. plumbing a level-based system on top of a cateogory-level-based system). On the other hand, prematurely optimizing for a future split that may never happen is a risk. (note: I don't think we're prematurely optimizing, because the kernel logger is so light-weight)A different approach that I'm happy to explore, and I think could make more sense than wiring low-level
kernelinto higher-levelnodelogging, is to ~move thekernel::Loggertoutil::Logger, and have all logging (instead of justkernel) go through these structured callbacks. This way, the low-level logging interface is inutil, and all the opinionated node stuff (i/o, ratelimiting, log formatting, ...) would live incommon. This would mean node logging fundamentally also becomes level-based (i.e. debug string formatting is all-or-nothing), but I think that's a pretty small price to pay?In the mvp branch, I chose new
KernelLog...macros just to keep things straightforward, and I'd be open to using macro/compile flag magic to minimize the diff there. However, I think the reality is that logging signatures in kernel code will have to change (for every single callsite) to make it context-aware, so I don't think a new macro name is all that important?I agree with most of your observations in this paragraph, and kernel macros should not be leaking into util, wallet, GUI. I think the proper solution here is to remove logging from util though. In my view, low-level (i.e. util) code should not be logging at all. I've implemented one way of doing that in master...stickies-v:bitcoin:2025-12/util-no-logging. It seems the scope of that change is rather limited, but there are many approaches possible, so that would warrant its own discussion.
sedited commentedon Dec 16, 2025
Thanks for taking charge of this! The C interface you laid out looks really nice too and should be easier to wire against whatever is required.
One point that might be nice to mention here too is that we are also defining a bunch of categories that the kernel should not have to know about.
Reading through your comment, I actually like what you are laying out as an alternative. Maybe a refinement of that idea could be to split the logger into a low-level and high-level implementation. Both can be implemented as global instances for now. The low-level class could take care of formatting, level detection, and providing a unified macro for all users. It can then feed into a higher level class through a callback. For a global logger, this would mean we'd retain a global low level logger, and introduce a global higher-level instance. We only initialize the global callback into the higher level logger when it is actually required by the respective binary, similar to what we do currently with the translations. For the kernel library itself, we can then surface this callback to the user through the API with the interface you are suggesting here.
I'm not really following why we can't have a set of categories defined to filter against pre formatting. Are you suggesting this, because we'd ideally want to split off non-kernel categories?
stickies-v commentedon Dec 16, 2025
That sounds like the new design I'm currently working on. So far, it seems like it'll be a much bigger code diff than the seperation branch (because a lot of
logging.{h,cpp}code will need to move around, but there should be 1) less duplication and 2) more contained logging logic, so I think overall this might work out nicely.Category filtering to me feels like a high-level (i/o) logging output concern rather than a low-level logging concern. We could make the low-level interface filter based on categories, but that I think would needlessly clutter the interface for users that don't need it (incl kernel), so I'd much rather keep it purely levels-based. So for e.g.
node, that would mean that if only a single debug category is enabled, all debug logs would be formatted*, but only the one enabled category printed. Unless measurements indicate it really is too much overhead, I think that's not going to be a big deal when you're in debug mode already?*note: there are 2 formatting steps: 1 the
LogInfo(fmt, args...)formatting, and then 2) the formatting where we produce a single string containing timestamp, level, msg, ... I'm generally talking about the first step. The second is of course only done when needed.stickies-v commentedon Dec 17, 2025
I have implemented a new approach ("Layering") in master...stickies-v:bitcoin:2025-12/kernel-logging-layering. The Layering approach turned out much less invasive than my initial attempts showed, and now has my preference.
In a nutshell:
util::log::Logger) and (higher-level) log sinks (BCLog::Sink,btck_LoggingConnection) that consume the logs by registering a callback on the logger.bitcoinkernel.hAPI is ~identical between both versions, as are theutil::log::Loggerandkernel::Loggerinterfaceslogging.{h,cpp}from kernelConceptually, it's quite similar to the earlier Separation approach, but just organizes the code differently, reducing code churn and duplication.
@ryanofsky I think this addresses your concerns about duplicated macros, about util code depending on kernel code, and about minimizing changes to validation and util code. It still introduces a new class, I don't immediately see an elegant way around that that also allows
kernelto not depend on node logging code. Does that seem like a reasonable enough trade-off to you to warrant opening a PR and discuss the details?I'll open a PR if/when people are happy about the concept and initial approach, that's probably a better place to discuss code in more depth.
sedited commentedon Dec 17, 2025
This newest approach looks good to me. I think some people might still have reservations about unconditionally logging all categories. I tend to agree with your opinion on this though. Once debug logging is activated, expectations on string serialization overhead should be the same, no matter the category selected.
ajtowns commentedon Dec 20, 2025
Gross. Changing all the validation logs prefix the macro with
Kerneland changingBCLog::tokernel::Categoryis not an improvement.That pretty much destroys the use case of Trace logging which is intended for high-rate logging that's probably not performant enough for regular usage. Which is fine today, because we only do trace logging in the wallet. But being able to do it for mempool analysis/debugging would also be useful.
Likewise for:
That's not the case.
As far as the problem statements go:
Outside of the kernel, the callbacks are only used in tests, so changing how the callbacks work seems trivial, not justification for a major rework.
That interface isn't a good design even for the node software. #34038 removes it from being exposed to users (via the
-loglevelconfig option), so that a followup can simplify it further.At that point, having
btck_logging_trace(VALIDATION)orbtck_logging_setlevel(VALIDATION, TRACE)to enable trace logging for validation at a cost of slower validation, seems fine to me.This seems exactly backwards to me; I would instead have approached it as "let's rip out the node specific stuff from logging into node/loghandler, and have it register itself in a similar way to how kernel users will register their log handling code".
ajtowns commentedon Dec 21, 2025
For example, ajtowns@57e6ba1
stickies-v commentedon Jan 5, 2026
I have updated the Issue description to reflect the new layering approach that minimizes churn and code duplication, wiring all logging into a low-level
util::log::Loggeraddressing review feedback (1, 2)@ajtowns thank you for the review, and apologies for the slow response - I was off for a little while. I wish I'd updated the Issue description earlier, because I think a lot of your concerns were already addressed in the new approach. Apologies for not doing that earlier.
Structured callbacks can be done with a much smaller diff indeed, but I think it doesn't address two concerns:
nodelogging logic is irrelevant forkernel(e.g. non-kernel categories, ratelimiting, string formatting, ...), so keepingnodelogging logic separate would be goodbitcoinkernellogging interface, forcing the user to do category configurationAgreed, see an earlier update (which is now the documented approach in this Issue).
My understanding of your concern is: "with level-only filtering at the low (
util::log::Logger) level, enablingTracemeans paying formatting overhead for all trace logs regardless of category, making high-frequency trace logging impractical." Is my understanding correct? I think we'll have to add a LOT moreTracelogging before that actually becomes a concern. I'm not sure we ever will, but if/when we ever need it, I think that can still be easily addressed later by adding a category filter predicate toutil::log::Logger? Optimizing for that now seems premature to me? (Just in case there's any confusion, my suggested approach still uses levels-based filtering to avoid unnecessary log string formatting.)stickies-v commentedon Jan 21, 2026
I've now opened #34374 to address this issue. Compared to the previous approach, the main changes are:
Dispatcher(previouslyutil::log::Logger) is now instantiated with aFilterFunc filterpredicate, that enablesutilto remain oblivious to thelogging.hlevel/category setting logic whilst still allowing log consumers (such asBCLog::Logger) to avoid overhead (e.g string formatting, lambda evaluation) when logs don't pass the filter. I think this should address @ajtowns' main outstanding concern? It also avoids the need forBCLog::Loggerto synchronize its minimum levels withutil::log::Dispatcher's minimum level, simplifying the implementation in some ways (but complicating it in others, e.g. requiring a new globalDispatcher).util::log::Loggertoutil::log::Dispatcherso we can keep theBCLog::Loggername unchanged and minimize unnecessary code churn.I'd be grateful to hear your feedback on this new approach on the PR. Also thanks to @ryanofsky for giving me ideas (offline) to refine the approach.
purpleKarrot commentedon Jan 27, 2026
The Kernel is not a service where multiple clients can connect to each with their own logger. Instead, it is a library that each client has unique control over. If clients want to write their logs to multiple places, they can do so with a single log handler. They can decide whether and how they want to filter their log messages based on level, category and/or source location, as long as all this information is made available to them.
I think the logging API after should be modelled after set_terminate in the C++ standard library:
That means:
NULL.set_terminate->set_log).stickies-v commentedon Jan 27, 2026
Client-side filtering was a big driver behind my thinking too, but I also agree with @ajtowns's concerns about not incurring unnecessary overhead for logs that are later dropped. I don't think a user who's interested only in Info level logs should be forced to pay for all the (sometimes expensive, e.g. calculating memory usage) Debug and Trace statements that exist in the codebase. Also keep in mind that kernel and node share a lot of code, so we have to make sure changes make sense for all subsystems. This has led me to the approach adopted in #34374, where filtering is possible but it's entirely up to the client to implement it, so most of this complexity is kept out of kernel.
I agree. I don't see the need for more than a single connection per context, but I think supporting multiple logging contexts makes sense given our current context-aware interface.
There is an argument to be made for that approach, but I think it is awkward that some part of the interface is context-aware (e.g. ChainstateManager) and another part (e.g. logging) is global. I think, to keep the conversations somewhat focused, your thoughts on having a single handler might best be channeled to #30342 (and see also e.g. #30338 for a previous discussion)
purpleKarrot commentedon Jan 27, 2026
My point is that you have a single, global logging context with multiple dependent receivers.
Consider the use case where a client wants to write info logs to the terminal and debug logs to a file. They can register two receivers, but setting the log level affects both, because both actually share the same single, global context.
I find it more awkward that logging attempts to provide multiple "connections" to a single, global context.
I see three valid approaches:
a) Support multiple independent receivers. Each receiver should respond to its own log level filtering.
b) Embrace logs being global, with a single receiver and a single setting for log level filtering.
c) Getting rid of global logging state. This is proposed in #30342
The current proposal seems to be a mix of a) and b), with multiple receivers that respond to a single, shared log level.
stickies-v commentedon Jan 27, 2026
I agree, but again, that's also the case on master, and not in scope for this proposal. Nothing changes here.
I disagree. The current proposal is orthogonal to the issue you're talking about here. The status quo on master is that we have a global logger, with a global config, and multiple connections that all hook into the same global logger. As far as I can tell, this proposal does not change anything about that. If you see it differently, can you point out what this proposal changes about the status quo wrt the global logger, its config and its connections?
Note: the original issue description mentioned this global logger awkwardness as one of kernel's current logging issues, but this was just informational and never something addressed in this RFC. I've now removed it reduce confusion.
ryanofsky commentedon Jan 27, 2026
I am sympathetic with purpleKarrot that it's a little awkward to support creating multiple logging receivers if the different receivers don't actually have many independent options, and setting an option on one of them sets it on all of them. If that's the case, probably better not to allow creating multiple receivers. Or not allow creating multiple receivers per log source.
This doesn't seem like a very major concern by itself though, and should be orthogonal to #34374 like stickies said.
I think #33847 and #30342 and are a pretty straightforward way out of the "We have to use a singleton here because of other singletons" dilemma (#34374 (comment)) and think those could move forward separately.
Merge bitcoin#34374: kernel: use struct-based logging and simplify lo…