Rust Log First Knowledge _rust

Source: Internet
Author: User
Objective

Log is often used, as in other languages, Rust also has log library log, the implementation of the log rating, log filtering, log output format, log rollback functions. GitHub address: Https://github.com/rust-lang-nursery/log rust Log

The most basic usage of log is achieved by 5 macros, respectively: [error!], [warn!], [info!], [debug!], [trace!].

Levels of the logging
pub enum loglevel {
    Error,         //error is the highest level of log rating
    Warn,
    Info,
    Debug,
    Trace,         //trace is the lowest level
}

For how to customize the rating, temporarily not involved, custom rating, when the project is large, involving many libraries, if more than one library custom rating, the last log is difficult to deal with, so when multiple libraries are involved in the log, different library log rating is different, there will be problems, so do not recommend custom log rating, Custom issues are considered only if the current rating does not meet the requirements of the project.

There is also a log filter associated with the log level, which will be used later:

Level filters of the logging Framework
Pub enum Loglevelfilter {off
    ,
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

The following example shows the most basic usage of class log:

#[macro_use]
extern crate log;

Use log::loglevel;//Log Level

fn main () {
    log_lever_fn ();
}

fn LOG_LEVER_FN () {Let
    data= ("Forty-two");
    Let private_data= "private";
    log! (Loglevel::error, "Received errors:{},{}", data.0,data.1);
    log! (Target: "App_events", Loglevel::warn, "app warning:{},{},{}", Data.0,data.1,private_data);

    Let (err_info,port) = ("No connection",);
    error! ("error:{} on port {}", err_info,port);
}

The following source code is somewhat difficult to read, you can refer to here: https://kaisery.gitbooks.io/rust-book-chinese/content/content/Macros%20%E5%AE%8F.html

Log macro Source code///this macro'll generically log with the specified ' LogLevel ' and ' format! '///based the argument
-list.
#[macro_export]
macro_rules! log {
    (target: $target: expr, $lvl: expr, $ ($arg: TT) +) => ({
        static _ LOC: $crate:: LogLocation = $crate:: loglocation {
            __line:line! (),
            __file:file! (),
            __module_path:module_ path! (),
        };
        Let lvl = $lvl;
        If LVL <= $crate:: __static_max_level () && lvl <= $crate:: Max_log_level () {
            $crate:: __log (LVL, $ Target, &_loc, format_args! ($ ($arg) +))
        }
    );
    ($LVL: Expr, $ ($arg: TT) +) => (log! ( target:module_path! (), $LVL, $ ($arg) +)
}

The following is the error macro source code, several other levels of macro similar, here are not listed, more source reference: https://docs.rs/crate/log/0.3.8/source/src/macros.rs

Logs a message at the error level.
#[macro_export]
macro_rules! error {
    (target: $target: Expr, $ ($arg: TT) *) => (
        log! ( Target: $target, $crate:: Loglevel::error, $ ($arg) *);
    ($ ($arg: TT) *) => (
        log! ( $crate:: Loglevel::error, $ ($arg) *)
}
Logger

Only log In many cases is not satisfied with functional requirements, log different functions to rely on different logger implementation, the following is currently more commonly used logger:simple minimal loggers:
ENV_LOGGER--A logger configured via an environment variable (log configuration via environment variables) Simple_logger Simplelog Stderrlog Flexi_logger Complex Configurable frameworks:
Log4rs Fern Adaptors For the other facilities:
Syslog Slog-stdlog

In practical engineering application, different logger should be chosen according to different requirements. Here's an example of Env_logger:

#[macro_use]
extern crate log;//Add the corresponding item to the CARGO.TOML [dependency] Item
a fn of the extern crate Env_logger;//logger

Main () {
    env_logger_fn ();
}

fn ENV_LOGGER_FN () {
    env_logger::init (). Unwrap ();//initializes the global logger with a env logger.
    Info! ("Starting up");
    error! ("error:{}", 404);
}
CARGO.TOML
[dependencies]
log = "0.3"
Env_logger = "0.4.3"

Compile, Run results:

The log level is set to info levels through environment variables, and the results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.