In the example bellow, the StdoutMessege struct works as intended by itself, however if I wrap it in a OnceLock I doesn't actually print anything, even though the function print gets called every time.
You aren't flushing the buffer there, so the program ends without the messages being written to Stdout. Normally a BufWriter flushes itself when it drops, but putting it into a static (via a OnceLock) means it doesn't get dropped. You should insert calls to flush() if you want StdoutMessage to be in a static.
You shouldn't normally need to use BufWriter with println!() or std::io::Stdout. They already do line buffering. If you want better performance you can call lock on Stdout to get a locked stdout handle rather than locking on every write.
FWIW, you should really be using writeln! instead of write_bytes(format!(...)), as it's significantly more efficient. Of course, that could be negligible in your app; I'm just trying to do my part to combat software bloat.