How it works...

Rust's logging system is based on the log crate, which provides a common facade for all things logging. This means that it doesn't actually provide any functionality, just the interface. The implementation is left to other crates, env_logger in our case. This split into facade and implementation is pretty useful, as anyone can create a cool new way of logging stuff which is automatically compatible with any crate.

The choice of logging implementation used should be up to the consumer of your code. If you write a crate, don't use any implementation but simply log all things via the log crate only. Your (or someone else's) executable that uses the crate can then simply initialize their logger of choice[9] in order to actually process the log calls.

The log crate provides the log! macro[11], which accepts a log Level, a message that can be formatted the same way as in println!, and an optional target. You could log stuff like this, but it's more readable to use the convenience macros for every logging level, error!, warn!, info!, debug!, and trace!, which all simply call log! in the background. A log's target[19] is an additional property that helps the logger implementation group the logs thematically. If you omit the target, it defaults to the current module. So, for example, if you logged something from the foo crate, its target would default to foo. If you logged something in its submodule foo::bar, its target would default to bar. If you then consumed the crate in a main.rs and logged something there, its target would default to main.

Another goodie that log provides is the log_enabled! macro, which returns whether or not the currently active logger is set to process a certain warning level. This is especially useful in combination with Debug logs that provide useful information at the cost of an expensive operation.

env_logger is a logger implementation provided by the Rust nursery. It prints its logs on stderr and uses pretty colors for different logging levels if supported by your Terminal. It relies on a RUST_LOG envvar to filter which logs should be displayed. If you don't define said variable, it will default to error, which means that it will print only the log level error from all targets. As you can guess, other possible values include warn, info, debug, and trace. These will, however, not only filter the specified level, but all levels above it as well, where the hierarchy is defined like this:

error > warn > info > debug > trace

This means that setting your RUST_LOG to warn will show all warn and all error logs. Setting it to debug will show error, warn, info, and debug.

Instead of error levels, you can set RUST_LOG to targets, which will show all logs of the selected targets regardless of their log levels. This is what we do in our example, we set RUST_LOG to logging in order to show all logs with a target called logging, which is the standard target for all logs in our binary. If you wanted, you could combine a level filter with a target filter like this: logging=warn, which would only show warn and error logs with a target of logging.

You can combine different filters with a comma. If you want all logs of this example to be displayed, you can set your variable to logging,extra_info, which filters for both the targets logging and extra_info.

Lastly, you can filter your logs by content with a slash (/), after which you can write down a regex that has to be matched. If you set RUST_LOG to logging=debug/expensive for instance, only logs with the logging level of debug and upwards with the target of logging that also contain the word expensive will be displayed.

Wow, that's a lot of configuration! I advise you to experiment a bit with the different filtering modes and run the example in order to get a feeling for how the parts fit together. If you need additional information, all possibilities for the value of RUST_LOG in the current version of env_logger are documented at https://docs.rs/env_logger/.

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

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