Loggers

The actual logger implementations that are contained in some crates are as follows:

  • env_logger
  • simple_logger
  • simplelog
  • pretty_env_logger
  • stderrlog
  • flexi_logger
  • log4rs
  • fern

It can be difficult to choose between these logger implementations. I recommend that you explore them on crates.io to learn how they differ. The most popular one is env_logger, which is the one that we are going to use. env_logger reads the RUST_LOG environment variable to configure logging and prints logs to stderr. There is also the pretty_env_logger crate, which is built on top of env_logger and prints logs with a compact and colorful format. Both use the same environment variable for configuration.

stderr is one of three standard streams—stdin, where your program reads the input data with the console; stdout, where the program sends the output data; and stderrwhich has the special purpose of showing errors or other information about working with the application. Loggers often use stderr to avoid affecting the output data. For example, let's say that you have a tool that decodes an input stream. You want the tool to send the decoded data only to the output stream. How will the program inform you about any issues it is experiencing? In this case, we can use the stderr stream, which works as an output stream, but doesn't pollute stdout? There is stderr stream that works as output stream, but doesn't pollute stdout.

Add the logger to the dependencies list of your Cargo.toml  file:

[dependencies]
log = "0.4"
pretty_env_logger = "0.2"
hyper = "0.12"
rand = "0.5"

Then add these types to your main.rs file:

use hyper::{Body, Response, Server};
use hyper::rt::Future;
use hyper::service::service_fn_ok;
use log::{debug, info, trace};
..................Content has been hidden....................

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