Opens an external site in a new window
Mental Health Awareness Month
“Community”
RODNEY LAB
  • Home
  • Plus +
  • Newsletter
  • Links
  • Profile
RODNEY LAB
  • Home
  • Plus +
  • Newsletter
  • Links

Rust Cloudflare Worker Logging 📋 # Rust Cloudflare Worker Logging 📋 #

blurry low resolution placeholder image Rust CI Tooling
  1. Home Rodney Lab Home
  2. Blog Posts Rodney Lab Blog Posts
  3. Rust Rust Blog Posts
<PREVIOUS POST
NEXT POST >
LATEST POST >>

Rust Cloudflare Worker Logging 📋 #

Published: 2 years ago
4 minute read
Gunning Fog Index: 4.4
Content by Rodney
blurry low resolution placeholder image Author Image: Rodney from Rodney Lab
SHARE:

📝 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 #

Please enable JavaScript to watch the video 📼

Rust Cloudflare Worker Logging

🗳 Poll #

Which logging service are you using for new projects?
Voting reveals latest results.

🖥 Rust Cloudflare Worker Logging: Code #

console_error_panic_hook #

src/utils.rs
rust
    
1 use cfg_if::cfg_if;
2
3 cfg_if! {
4 // https://github.com/rustwasm/console_error_panic_hook#readme
5 if #[cfg(feature = "console_error_panic_hook")] {
6 extern crate console_error_panic_hook;
7 pub use self::console_error_panic_hook::set_once as set_panic_hook;
8 } else {
9 #[inline]
10 pub fn set_panic_hook() {}
11 }
12 }
src/lib.rs
rust
    
1 mod utils;
2
3 /* TRUNCATED... */
4
5 #[event(fetch)]
6 async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
7 utils::set_panic_hook();
8
9 /* TRUNCATED... */
10 }
Cargo.toml
toml
    
11 # ...TRUNCATED
12
13 [lib]
14 crate-type = ["cdylib"]
15
16 [features]
17 default = ["console_error_panic_hook"]
18
19 [dependencies]
20 cfg-if = "1.0.0"
21 chrono = "0.4.24"
22 chrono-tz = "0.8.2"
23 console_error_panic_hook = { version = "0.1.1", optional = true }
24 reqwest = { version = "0.11.18", features = ["json"] }
25 rmp = "^0.8"
26 rmp-serde = "1.1.1"
27 serde = "1"
28 worker = "0.0.17"
29
30 # TRUNCATED...

Logtail Request Log Example Code #

src/lib.rs — click to expand code.
src/lib.rs
rust
    
1 mod utils;
2
3 use chrono::{DateTime, Utc};
4 use reqwest::header::CONTENT_TYPE;
5 use rmp_serde::Serializer;
6 use serde::{Deserialize, Serialize};
7 use std::collections::HashMap;
8 use worker::*;
9
10 #[derive(Debug, PartialEq, Deserialize, Serialize)]
11 struct LogDataItem {
12 pathname: String,
13 request_details: String,
14 }
15
16 #[derive(Debug, PartialEq, Deserialize, Serialize)]
17 struct LogData {
18 dt: String,
19 level: String,
20 message: String,
21 item: LogDataItem,
22 }
23
24 async fn log_request(req: &Request, env: &Env) {
25 let pathname = req.path();
26 let timestamp = Date::now().to_string();
27 let request_details = format!(
28 "{timestamp} - [{pathname}], located at: {:?}, within: {}",
29 req.cf().coordinates().unwrap_or_default(),
30 req.cf().region().unwrap_or("unknown region".into())
31 );
32 console_log!("{request_details}");
33
34 let date_time: DateTime<Utc> = match DateTime::parse_from_rfc3339(&timestamp) {
35 Ok(value) => value.into(),
36 Err(_) => Utc::now(),
37 };
38
39 let log_data = LogData {
40 dt: date_time.to_rfc3339(),
41 level: String::from("info"),
42 message: "worker request".to_string(),
43 item: LogDataItem {
44 pathname,
45 request_details,
46 },
47 };
48
49 let mut buffer = Vec::new();
50 log_data
51 .serialize(&mut Serializer::new(&mut buffer).with_struct_map())
52 .expect("Unable to serialize log data");
53 let logtail_source_token = env
54 .secret("LOGTAIL_SOURCE_TOKEN")
55 .expect("LOGTAIL_SOURCE_TOKEN must be defined")
56 .to_string();
57
58 let client = reqwest::Client::new();
59 let _ = client
60 .post("https://in.logs.betterstack.com")
61 .header(CONTENT_TYPE, "application/msgpack")
62 .bearer_auth(logtail_source_token)
63 .body::<Vec<u8>>(buffer)
64 .send()
65 .await;
66 }
67
68 // TRUNCATED...
69
70 #[event(fetch)]
71 async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
72 utils::set_panic_hook();
73
74 log_request(&req, &env).await;
75
76 // TRUNCATED...
77 }

Message Pack Crates #

Cargo.toml
rust
    
11 # ...TRUNCATED
12 [dependencies]
13 cfg-if = "1.0.0"
14 chrono = "0.4.24"
15 chrono-tz = "0.8.2"
16 console_error_panic_hook = { version = "0.1.1", optional = true }
17 reqwest = { version = "0.11.18", features = ["json"] }
18 rmp = "^0.8"
19 rmp-serde = "1.1.1"
20 serde = "1"
21 worker = "0.0.17"
22
23 # TRUNCATED...

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.

blurry low resolution placeholder image ask Rodney X (formerly Twitter) avatar

Rodney

@askRodney

Just dropped a new video on adding request logging to a 🦀 Rust Cloudflare worker using

@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

— Rodney (@askRodney) June 20, 2023

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.

Thanks for reading this post. I hope you found it valuable. Please get in touch with your feedback and suggestions for posts you would like to see. Read more about me …

blurry low resolution placeholder image Rodney from Rodney Lab
TAGS:
RUSTSERVERLESS

Related Posts

blurry low resolution placeholder image Using raylib with Dear ImGui: Game Dev Debugging UI 🐜

Using raylib with Dear ImGui: Game Dev Debugging UI 🐜

c++
gaming
<PREVIOUS POST
NEXT POST >
LATEST POST >>

Leave a comment …

Your information will be handled in line with our Privacy Policy .

Ask for more

1 Nov 2022 — Astro Server-Side Rendering: Edge Search Site
3 Oct 2022 — Svelte eCommerce Site: SvelteKit Snipcart Storefront
1 Sept 2022 — Get Started with SvelteKit Headless WordPress

Copyright © 2020 – 2025 Rodney Johnson. All Rights Reserved. Please read important copyright and intellectual property information.

  • Home
  • Profile
  • Plus +
  • Newsletter
  • Contact
  • Links
  • Terms of Use
  • Privacy Policy
We use cookies  to enhance visitors’ experience. Please click the “Options” button to make your choice.  Learn more here.