📝 Rust Worker Logging #
In this video, we continue the Rust Worker series, zooming in on Rust Cloudflare worker logging.
Previously, we saw how you can use the console_log
procedural macro
to log and help debug in a local, dev environment. Here, though, we advance, both looking at getting
more useful output when the Rust code panics, and also some tooling for request logging, while running
in a production environment. There, we see some Cloudflare tooling, as well as how you might set
up Logtail as a logging service in your worker.
If that’s what you wanted to know, then hit play on the video! After that, don’t forget to check links below. Drop a comment below or reach out for a chat on Element as well as Twitter @mention if you have suggestions for improvements or questions.
📹 Rust Cloudflare Worker Logging: Video #
🗳 Poll #
🖥 Rust Cloudflare Worker Logging: Code #
console_error_panic_hook
#
Logtail Request Log Example Code #
src/lib.rs
— click to expand code.
Message Pack Crates #
Follow the link in the next session for the full code.
🔗 Rust Cloudflare Worker Logging: Links #
- Article on getting started with Rust Cloudflare workers
- GitHub repo with full code
- console_error_panic_hook docs
- rmp_serde (Rust Message Pack) crate docs
- Logtail signup
- Cloudflare Logpush
- wrangler tail command docs
- Twitter handle: @askRodney
🏁 Rust Cloudflare Worker Logging: Summary #
Is there a source map feature for Rust WASM? #
- When writing code in Rust and compiling to WASM, if the WASM module panics, you should see a console error. This will be in the browser or Terminal, depending on the environment you are running the module in. Either way, by default, the error message will show you which line of WASM code the error originated from. This is not ideal, as the Rust source will be where you want to focus your debugging efforts! console_error_panic_hook is a crate you can run in debug mode to output the line of Rust code which caused the panic. Check the project GitHub page (https://github.com/rustwasm/console_error_panic_hook#readme) to get going with it.
Is there a Rust Message Pack crate? #
- rmp_serde is a popular and useful Rust crate for generating Message Pack data arrays. We saw that you might decide to use it with a logging service, like Logtail. That is because Message Pack provides a more efficient data structure than the JSON alternative.
How can you use Logtail with Rust WASM? #
- Although Logtail does not have a Rust SDK (at the time of writing), it does have a REST API. You can use the Rust reqwest package to push your request logs into the Logtail dashboard. For convenience, you might also like to use the rmp_serde crate, which can serialize your log data into the Message Pack format, for efficient transport.
🙏🏽 Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Then get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Just dropped a new video on adding request logging to a 🦀 Rust Cloudflare worker using
— Rodney (@askRodney) June 20, 2023
@LogTailHQ.
We also see how to get more useful error messages for Rust WASM panics.
Hope you find it useful!#learnrust #askRodneyhttps://t.co/WFV1lGiysE
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as SvelteKit. Also, subscribe to the newsletter to keep up-to-date with our latest projects.