Logging in a Rust application
This blog post is part of a series I’m developing documenting my evolution as a Rust developer. Currently I’m a newbie, but I’m making progress.
As a newbie, please feel free in the comments to elaborate on what I might be doing wrong, could do better or is not “canonical” Rust.
Logging is obviously a key aspect of a production-ready application. While one could use println!
or dbg!
or similar Rust macros to achieve something similar, they are not really a replacement for a real logging framework. In fact, particularly for (long running) “services” as opposed to CLIs, many architecture/coding standards prohibit use of the equivalent to println!
in whatever language you’re using. I myself have set such standards on projects I’ve lead.
So as I’ve been teaching myself Rust, I was naturally interested in what the Rust ecosystem has in terms of logging capabilities.
The first thing to mention is log crate. This library provides a standard logging facade API that is then combined with an actual logging implementation provided by a separate crate that you as the developer can select per your requirements. This is similar to commons-logging
or SLF4J
in the Java world. To use log
in Rust,
$ cargo add log