There's more...

If you've never worked with a logger before, you might wonder what the difference between certain log levels is. Of course, you can use them for whatever purpose you want, but the following conventions are usual for loggers in many languages:

Log level Usage Example
Error Some major problem occurred that might terminate the program soon. If the application is a service that should always run, a system administrator should immediately be notified. The connection to the database has been broken.
Warn An issue that's not severe or has an automatic workaround has happened. Someone should check this out at some point and fix it. A user's configuration file contains unrecognized options that have been ignored.
Info Some information that might be useful to look at at a later point. This logs normal conditions. The user has started or stopped a process. A default value has been used because no configuration has been provided.
Debug Information that is helpful to programmers or sysadmins when trying to fix a problem. Contrary to many other languages, a debug log is not removed in release builds. The parameters passed to a major function. The current state of the application at various points.
Trace Very low-level signals of control flow that are only useful to a programmer trying to chase a bug. Allows the reconstruction of a stack trace. The parameters of a minor helper function. The beginning and end of a function.

 

Many languages also contain a Fatal log level. In Rust, a good old panic!() is used for that. If you want to log your panics in some special way as well, you can replace the usual reaction to a panic by simply printing it to stderr by calling std::panic::set_hook() with whatever functionality you want. An example of what this might look like is as follows:

    std::panic::set_hook(Box::new(|e| {
println!("Oh noes, something went wrong D:");
println!("{:?}", e);
}));
panic!("A thing broke");

A good alternative to env_logger is the slog crate, which provides excellent extensible structured logging at the cost of a steepened learning curve. Plus, its output looks pretty. If that sounds interesting, be sure to check it out at https://github.com/slog-rs/slog.

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

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