Repository navigation
Description
edit 2026-03: This issue was previously intended for using the Drain Algorithm, but I think we can do better (and it's easier) by parsing the log staements in the source code of Bitcoin Core. This is discussed further down below.
With the log-extractor @m4ycon added, we have a stream of log messages that we could use for e.g. anomaly detection. I've experimented a bit with the Drain algorithm (Drain: An Online Log Parsing Approach with Fixed Depth Tree) to parse logs and was surprised by the fairly minimal work required to parse a debug.log with 8.7M entries down to a 231 templates: https://gist.github.com/0xB10C/4e6ad297ad698aa42c08164b31500622
$ head debug.log-20260107-hal_templates.csv
EventId,EventTemplate,Occurrences
cfd83b52,received: <*> <*> bytes peer <*>,1177351
bab6597b,got inv: wtx <*> <*> peer <*>,3329366
2696937b,Requesting wtx <*> peer <*>,442336
b31d2e19,sending <*> <*> bytes peer <*>,1888676
49aa8dc6,Enqueuing TransactionAddedToMempool: txid <*> wtxid <*>,441394
c56e0b70,"AcceptToMemoryPool: peer <*> accepted <*> wtxid <*> poolsz <*> txn, <*> kB",441394
43942aab,TransactionAddedToMempool: txid <*> wtxid <*>,441394
1957b746,"socket closed, disconnecting peer <*> peeraddr <*>",9710
f0fea8d0,Resetting socket for peer <*> peeraddr <*>,11191
I've used the Python example from https://github.com/logpai/logparser for this (ty for telling me about this @jharveyb!). With more pre-processing (masking hashes such as block header hashes + txids + wtxids, peer ids, IP addresses, .onion and .i2p addresses, ...) I think we can get to a good starting point for having good Bitcoin Core debug.log templates to work with in anomaly detection.
It probably makes sense to experiment with https://github.com/logpai/Drain3 as it supports persistence (of the tree) and streaming logs into it. A good starting point is probably https://github.com/logpai/Drain3/blob/master/examples/drain_bigfile_demo.py
Then, a next step could be to implement Drain in e.g. Rust (or use https://github.com/haydenflinner/drainrs / https://github.com/benwtrent/drain-rs) and tune it for usage with Bitcoin Core debug.logs. Also, implementing Bitcoin Core specific pre-processing logic should be helpful for good results.
Very basic anomaly detection could be done by looking at the number of occurrences of templates per timeframe (e.g. 10min/hour). Large standard deviations could indicate anomalies. Additionally, log messages that only rarely occur are probably interesting to look at. This is something that could be implemented in e.g. a metrics-tool like Prometheus exporter.
From there on, exploring e.g. techniques and tools from https://github.com/logpai/loglizer could be considered.
Sample logs that can be used for this: https://demo.peer.observer/debug-logs/
Activity
0xB10C commentedon Jan 10, 2026
An obvious alternative could be to implement (optional) structured logging for Bitcoin Core. This would allow skipping the whole parsing part. I think this could be a worthwhile project, but out-of-scope for this issue.
0xB10C commentedon Jan 10, 2026
I found this Literature Review on Log Anomaly Detection Approaches Utilizing Online Parsing Methodology talk interesting too. Specially this table:
SwissLog and NuLog might be alternatives to Drain.
However, there seems to be a lot more literature on this. Could even be a topic for a thesis?
0xB10C commentedon Jan 10, 2026
I have historical logs that I'd be a) happy to provide for development and benchmarking of something like this and b) would want to run through this to look for rare messages and anomalies: https://bnoc.xyz/t/offer-detailed-bitcoin-core-node-debug-logs-2024/36/2
edilmedeiros commentedon Jan 12, 2026
Curious what I thought about this that touches the Core codebase.
0xB10C commentedon Jan 13, 2026
I'm not 100% sure I understand your question. From my quick-and-dirty experiments I found the following preprocessing helpful:
replace("=", "= ")(often used in e.g.peer=id)replace("(", "( ")replace(")", " )"){IP}) using a regexwtxid=[a-f0-9]{64}andtxid=[a-f0-9]{64}with markers0xB10C commentedon Jan 13, 2026
Another approach I tried to generate templates was to use
libclangto generate the compilation AST from the source code of Bitcoin Core and then piece together the log statements. This might work, but I think I found an even simpler solution:Use clang to get the source code as individual tokens and piece these together into the log statements. The following is a quick-and-dirty implementation of this.
For
net_processing.cpp, it produces the following output:By replacing the
%d,%s, ... with (regex) wildcards, I think we can probably match a log line to log statements quite well and even extract the argument names and values from it.These will probably change across different versions, but most of the log statements should be good for two or three major versions?
jharveyb commentedon Jan 16, 2026
Awesome results so far! I'm happy these tools are proving useful.
This one looks cool compared to SwissLog, AFAIU we wouldn't need to manually train a dictionary first. Though I suppose building one from old logs would be fine.
It seems like there was a follow-up paper to the Lit. Review you posted:
https://ieeexplore.ieee.org/abstract/document/10496061
Which has another useful comparison table of different implementations and techniques.
I would think that we actually want to retain the full addresses here, but also add a marker? Maybe IPV4 and IPV6 for those address, as the onion and I2P connection strings already have a suffix.
But keeping the addresses may help detect a peer behaving anomalously.
This is sick! Seems helpful for adding richer info, and also quite portable to other projects if needed.
I'll keep looking around for other anomaly detection tools.
davidgumberg commentedon Jan 31, 2026
I also want good tools for handling debug logs less in the interest of anomaly detection / monitoring and more in the interest of extracting data from logs, I agree on structured logging in Bitcoin Core as the ideal solution to this, but meanwhile:
https://github.com/davidgumberg/leave.logalone
Here is an example of this script's output taken from current master at bitcoin/bitcoin:
https://gist.github.com/davidgumberg/ccdf804c536acaddaccfde0135e61a12
{ "fmt": "Not connected to Tor control port %s, retrying in %.2f s\\n", "regex": "Not connected to Tor control port (\\S+), retrying in %\\.2f s\\\\n", "regex_types": [ "str" ], "args": [ "m_tor_control_center", "reconnect_timeout" ], "file": "src/torcontrol.cpp", "line": 643, "column": 5, "macro": "LogDebug", "category": "BCLog::TOR" },I took a stab at it and came to the same conclusion as @0xB10C above, that using libclang to navigate the AST is not worth it, there just isn't enough functionality exposed in libclang to make this approach viable in a way that isn't awful. But, libclang kindly provides a tokenizer that can be used for this purpose. I "use" the AST, but not really, as I basically just look for macros named
Log.*and then manually parse the tokens, I'll drop the AST stuff soon in exchange for token searching as in above: #336 (comment)As-is this script works pretty well, but
WalletLogPrintf()and any other non-macro use of log functions won't work well for now, on account of the AST woes, but this can be fixed with token parsing, which I'll change my script to at some point and special handling will be needed for the LOG_TIME_MICROS.* / BCLog::Timer RAII style logs, but that's the case for any approach, and shouldn't be too bad to deal with.Some other nice-to-haves I'm thinking for this tool/library would be:
Easy for a user to specify a log message they are interested in. Not ideal, but maybe via regex (LOL). But: this regex will just have to be a partial match and unique to one fmt str, not have all the match patterns etc. e.g. you do something like:
The user can access the variable values by index
The user should be able to access the matches as dict entries:
Easy if the argument is a named variable:
Hard if the argument is an expression!
Can be mixed for messages that have both named args and expressions,
access the named args by keys (and index) and access all the expression
args by index only.
Maybe? User can specify custom dict keys for the matches, e.g.:
Maybe? Can use a tiny LLM to generate keynames? Something like
qwen2.5coder-0.5b would do a good job inventing a name and can run on
pretty minimal hardware, but how would the user discover the variable
name? In that case couldn't they just as well have picked their own names?
You should be able to build and use log message db's for multiple versions, it
would be trivial to read the startup log message and generate / select a log
message index based on the commit hash / version name.
pandas?
0xB10C commentedon Mar 11, 2026
Noting down what I think are next steps here. I think its is a bit of a bigger effort and there's a lot still to figure out.
Here's a rough schema and example what I think an event could look like. Here, as a JSON object, but this would be a protobuf message for the other peer-observer tools to consume.
{ "log": "Requesting block 000000000000000000011a0fedc5c137037e3a4b3716316ba14237e40446e2d8 from peer=1337" "statement": "Requesting block %s from peer=%d", "args": { "hash.ToString()": "000000000000000000011a0fedc5c137037e3a4b3716316ba14237e40446e2d8", "peer_id": 1337, }, }[-]log-extractor: parse Bitcoin Core debug.log log messages with `Drain` and detect anomalies[/-][+]log-extractor: parse Bitcoin Core debug.log log messages[/+]0xB10C commentedon Mar 11, 2026
(also removed the Drain part from the title. I think Drain is a valid approach, but probably easier to go with the source-code parsing approach first)
0xB10C commentedon May 25, 2026
There's some discussion about how structured logging could look like in Bitcoin Core: bitcoin/bitcoin#35369
davidgumberg commentedon Jun 2, 2026
Just wanted to share an update on the tool I've been working on https://radicle.network/nodes/iris.radicle.network/rad%3Az37pH1UAxFvazXnfAMS5qbcUjQaP6
I have implemented approximately something pulled from the interfaces I described above, I'm using a callback here because the use case I am building for needs stateful parsing, but there maybe is a better interface for those that don't need state, here is what an example application looks like:
I don't know if this suits your needs @0xB10C as it is not in rust, but soliciting feedback on if this is in the right direction for an interface.
0xB10C commentedon Jun 8, 2026
I've been experimenting with a Drain-like implementation using a debug.log specific tokenizer a bit. A WIP tool can be found in https://github.com/peer-observer/debug-log-tool
Basically, running a debug.log through it gives an output similar to the following:
This works well. It needs a bit of fine tuning on the tokenization side and I need to think a bit more about how to handle
receive version message: <USERAGENT>where the user agent can contain spaces, be empty, have symbols, ...It contains a drnlog library, that we could use in peer-observer for real-time log template extraction. We can save the templates and build a grammar out of them. It "learns" about new log messages automatically.