{"id":11815,"date":"2025-09-06T21:53:25","date_gmt":"2025-09-06T21:53:25","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11815"},"modified":"2025-09-06T21:53:25","modified_gmt":"2025-09-06T21:53:25","slug":"logging-with-rust","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/logging-with-rust\/","title":{"rendered":"Logging with Rust"},"content":{"rendered":"<p>Rust supports a logging facade, which we can include using the following dependency in Cargo.toml<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;dependencies]\r\nlog = &quot;0.4.28&quot;\r\n<\/pre>\n<p>Now in our main.rs we can use the various levels of logging like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse log::{info, warn, error, trace, debug, LevelFilter};\r\n\r\nfn main() {\r\n    debug!(&quot;Debug log message&quot;);\r\n    trace!(&quot;Trace log message&quot;);\r\n    info!(&quot;Info log message&quot;);\r\n    warn!(&quot;Warning log message&quot;);\r\n    error!(&quot;Error log message&quot;);\r\n}\r\n<\/pre>\n<p>If you run this, nothing will be output because we need to add a logging provider. <\/p>\n<p>One simple provider is <em>env_logger<\/em> which will log to standard out. To include, add the following to the Cargo.toml dependencies<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nenv_logger = &quot;0.11.8&quot;\r\n<\/pre>\n<p>We&#8217;ll need to add the use clause<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse env_logger::{Builder, Env};\r\n<\/pre>\n<p>and then we need to initialise the env_logger, we can use the following at the start of the main function<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nenv_logger::init();\r\n<\/pre>\n<p>This will only output ERROR messages, we can change the log level using the environment variable like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nRUST_LOG=trace\r\n<\/pre>\n<p>Alternatively we can set the environment variable within code by replace the environment variable and the <em>env_logger::init();<\/em> line with<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet env = Env::default().filter_or(&quot;RUST_LOG&quot;, &quot;trace&quot;);\r\nBuilder::from_env(env).init();\r\n<\/pre>\n<p>or we can set in code instead using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nBuilder::new()\r\n   .filter_level(LevelFilter::Trace)\r\n   .init();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rust supports a logging facade, which we can include using the following dependency in Cargo.toml &#x5B;dependencies] log = &quot;0.4.28&quot; Now in our main.rs we can use the various levels of logging like this use log::{info, warn, error, trace, debug, LevelFilter}; fn main() { debug!(&quot;Debug log message&quot;); trace!(&quot;Trace log message&quot;); info!(&quot;Info log message&quot;); warn!(&quot;Warning log message&quot;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-11815","post","type-post","status-publish","format-standard","hentry","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11815"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11815\/revisions"}],"predecessor-version":[{"id":11816,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11815\/revisions\/11816"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}