Logging in Rust

Rust has quite a few flexible and extensive logging solutions. Like popular logging frameworks in other languages, the logging ecosystem here is split into two parts:

  • Logging facade: This part is implemented by the log crate and provides an implementation agnostic logging API. While other frameworks implement logging APIs as functions or methods on some object, the log crate provides us with macro-based logging APIs, which are categorized by log levels to log events to a configured log output.
  • Logging implementations: These are community developed crates that provide actual logging implementation in terms of where the output goes and how it happens. There are many such crates, such as env_logger, simple_logger, log4rs, and fern. We'll visit a couple of them in a moment. Crates that come under this category are meant to be used only by binary crates, that is, executables.

This separation of concerns between the logging API and the underlying mechanism by which logs go to an output is done so that developers don't need to change their log statements in code and can easily swap the underlying logging implementation on an as-needed basis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset