Repository navigation
Description
At the recent coredev it was suggested that it would be helpful to have logging output be more structured. This would go further than #34374 in that not only the internal metadata (time, category, level, source location, thread name) would be provided in a structured way, but so would per-message data, so that eg:
2026-05-24T01:56:29.650279Z [net] got inv: wtx 0629cda06a87a145a229417ba49b564779c7ddafd3cd4c1b388e99d0a059b845 have peer=58556
might instead be reported (perhaps via a -logjson=1 option) as
{"t": 1779587789650279, "c": "net", "l": "debug", "m": "got inv",
"p": {
"wtx": "0629cda06a87a145a229417ba49b564779c7ddafd3cd4c1b388e99d0a059b845",
"have": 1,
"peer": 58556
}
}Internally, that might be represented by adding std::vector<KV> kvs; to util::log::Entry, instead of putting the data in the Entry::message field, eg.
Streaming structured log messages like these over zmq, or storing them directly to a database might also be plausible and enable interesting features.
On the consumer side, I think this is relatively straightforward: include a vector<KV> in util::log::Entry where KV has key/value strings. The only complication is if you'd like to be able to distinguish values that are meant to be numbers, and could be represented in json as "foo": 1.23, in which case adding an "intended type" enum could be worthwhile.
On the producer side, I think there are a few options for how we could make this work. For example:
// current code for comparison:
LogDebug(BCLog::NET, "got inv: %s %s peer=%d", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
// parse printf-style format strings ourselves:
LogDebug(BCLog::NET, "got inv wtx=%s have=%d peer=%d", inv.ToString(), fAlreadyHave, pfrom.GetId());
// switch to std::format syntax but parse ourselves:
LogDebug(BCLog::NET, "got inv wtx={} have={:d} peer={:d}", inv.ToString(), fAlreadyHave, pfrom.GetId());
// custom syntax for providing parameters based on user-defined literals:
LogDebug(BCLog::NET, "got inv", "wtx"_LP(inv.ToString()), "have"_LP(fAlreadyHave), "peer"_LP(pfrom.GetId()));
// custom syntax for providing parameters based on macros:
LogDebug(BCLog::NET, "got inv", LP("wtx", inv.ToString()), LP("have", fAlreadyHave), LP("peer", pfrom.GetId()));
Activity
ajtowns commentedon May 25, 2026
My opinions, fwiw:
I think the
std::formatapproach would probably be a pain, since it would suggest thatwtx={1} have={0}should allow switching parameters around which would probably be annoying to support, and it would also require changing every format string, for not terribly much benefit as far as I can see.The two
LPapproaches are nice in that they put the key and value together making it harder to create mismatches, but are also require big changes to every log call, and seem a bit ugly to me, particularly when adding formatting to them. So I lean towards the "parse printf-style format strings ourselves" option.0xB10C commentedon May 25, 2026
Thanks for opening this for discussion! I think this is an interesting area to explore as it's a human-friendly (some humans can read JSON) machine-to-machine interface for internal node events.
Linking to a previous discussion and experimentation about parsing logs with e.g. a machine learning algorithm called Drain OR Python scripts to extract the possible log lines with their arguments from source code with e.g.
clang.cindex: peer-observer/peer-observer#336. The obvious alternative to manual parsing of log lines would be to have structured logging.In peer-observer/peer-observer#336 (comment), I've been using the parameter names as argument names. It would require to do stringification, which I don't know if it's support everywhere. For your
got invit would be:{"t": 1779587789650279, "c": "net", "l": "debug", "m": "got inv", "p": { "inv.ToString()": "0629cda06a87a145a229417ba49b564779c7ddafd3cd4c1b388e99d0a059b845", "fAlreadyHave?\"have\":\"new\"": 1, "pfrom.GetId()": 58556 } }These keys are obviously a bit weird and might change over time as variables and functions are renamed. One solution could be to assign them to more readable names and then pass these. That's more boilerplate code. Could look something like this.
resulting in
{"t": 1779587789650279, "c": "net", "l": "debug", "m": "got inv", "p": { "wtx": "0629cda06a87a145a229417ba49b564779c7ddafd3cd4c1b388e99d0a059b845", "have": 1, "peer": 58556 } }ajtowns commentedon May 26, 2026
Had a go at making this work, and it looks pretty plausible to me: https://github.com/ajtowns/bitcoin/commits/202605-log-structured/
EDIT: or, after updating the code:
{"t":1779782499995176,"c":"net","l":"debug","m":"received","k":{"msg":"pong","bytes":8,"peer":3}} {"t":1779782499995338,"c":"net","l":"debug","m":"received","k":{"msg":"headers","bytes":82,"peer":3}} {"t":1779782499995406,"c":"net","l":"debug","m":"sending sendheaders (0 bytes)","k":{"peer":3}} {"t":1779782499995557,"c":"net","l":"debug","m":"received","k":{"msg":"sendheaders","bytes":0,"peer":3}}m4ycon commentedon May 28, 2026
Hello, if I may contribute to this discussion, the first time that I saw this "structured logging" concept it was in this post, where it discuss this in a web server context. One idea from it that I find interesting, is the use of a context per request that accumulates information (that usually would be on separate logs), so then it logs only one thing at the end of the request with all the accumulated information. So when someone is debugging it, they still have all the information to "redo" the code path but with a single, compact log -- you can also see that as an event.
I don't know how well this idea fits into core code, I suppose p2p messages could be related to that, as could rpc requests and similar ones. Probably this could change many things related to logs that seem to be outside the scope of this issue. But anyway, it would be cool to see something similar here.
Also, if there's anything that I can help with this issue, I'd love to -- maybe not necessarily with a PR/commit since you guys have a greater understanding of cpp and more specifically about core (and it seems aj already have some implementation of it). But anything else you guys have in mind I could investigate it further (
and maybe save you some time idk).As I'm working with log-extractor from peer-observer, this might affect/help us in some way.
hulxv commentedon May 29, 2026
we can make this approach simpler and provide a macro for naming the parameters instead of using temp variables for them. also instead of this meaningless
m, we can embed the full message after formattingsomething like that:
output:
This approach is insipred by tracing-rs crate in Rust: