12 unstable releases (4 breaking)

0.4.0 Jun 27, 2024
0.3.0 Apr 8, 2024
0.2.0 Apr 23, 2023
0.1.8 Apr 7, 2023
0.0.2 May 15, 2021

#128 in Debugging

30 downloads per month
Used in 6 crates

MIT/Apache

29KB
775 lines

wd_log: A practical log crate for rust

wd_log GitHub Actions wd_log on crates.io wd_log on docs.rs License

This is a Rust Log Crate. Log output is mainly achieved through macros. Log composite output will be supported in future releases. The following features are now supported.

  • configurable
  • color identification
  • output to file
  • combination field (coding)

The current version is preliminary and will get better in the future.

Getting Started

wd_log is available on crates.io. It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.

At the point of the last update of this README, the latest published version could be used like this:

Add the following dependency to your Cargo manifest...

[dependencies]
wd_log = "0.1"

Example

config

Setting in multithreading is not supported

    //Set log level, default:Debug
    wd_log::set_level(wd_log::INFO);
    //Set the log output prefix, default:"wd_log"
    wd_log::set_prefix("WDLOG");
    //Whether to display the print time, default:true
    wd_log::show_time(false);
    //Whether to display the location, default:true
    wd_log::show_file_line(false);
    //Set output to a file, default:stdout
    wd_log::output_to_file("./log.txt").expect("file open failed");

print

  • By default there is no line break; macros ending in '_ln' indicate line breaks.
    wd_log::log_debug!("{}","hello world");
    wd_log::log_debug_ln!("{}","hello world");
    wd_log::log_info!("{}","hello world");
    wd_log::log_info_ln!("{}","hello world");
    wd_log::log_warn!("{}","hello world");
    wd_log::log_warn_ln!("{}","hello world");
    wd_log::log_error!("{}","hello world");
    wd_log::log_error_ln!("{}","hello world");
  • Allows empty and formatted printing
    wd_log::log_debug!();
    wd_log::log_debug!("{} {}","hello","world");
  • panic
    //After printing, trigger panic.
    wd_log::log_panic!("{}","hello world");

result type deal

  • Macros that begin with 'res_' can handle the result type
  • if result is Err, print an error and return None
  • if result if Ok(T), direct return Some(T)
    let result = Err("hello");
    let _res:Option<()> = wd_log::res_error!(result);
    let _res:Option<()> = wd_log::res_error_ln!(result);

    let _res:Option<()> = wd_log::res_error!(result;"this is {}","error");
    let _res:Option<()> = wd_log::res_error_ln!(result;"this is {}","error");

    let result:Result<&str,&str> = Ok("world");
    let res:Option<&str> = wd_log::res_error!(result);
    assert_eq!(res,Some("world"))
  • if result is Err, res_panic will panic after printing this error
  • if result is Ok(T), direct return Some(T)
        let result:Result<&str,&str> = Ok("hello");
        let res = wd_log::res_panic!(result;"succes");
        println!("{}",res);
        let result = Err("hello");
        //It can lead to panic
        let _ = wd_log::res_panic!(result;"this is {}","error");

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–2MB
~33K SLoC