Logging errors
In Rust, there's a log crate that provides a facade for application logging. The log provides five macros: error!
, warn!,
info!
, debug!
, and trace!
. An application can then create a log based on the severity and filter what needs to be logged, also based on the severity. For example, if we filter based on warn
, then we only log error!
and warn!
and ignore the rest. Since the log crate does not implement the logging itself, people often use another crate to do the actual implementation. In the documentation for the log crate, we can find examples of other logging crates that can be used: env_logger
, simple_logger,
simplelog
, pretty_env_logger
, stderrlog
, flexi_logger
, log4rs
, fern
, syslog
, and slog-stdlog
.
Let's implement custom logging in our application. We will use the fern
crate for logging and wrap that in async_log
to make logging asynchronous:
- First, add these crates in
Cargo.toml
:async-log = "2.0.0" fern = "0.6" log = ...