3个不稳定版本
| 0.7.0 | 2023年1月10日 | 
|---|---|
| 0.6.0 | 2023年1月10日 | 
#627 in 调试
659次每月下载
用于genominicus
26KB
358 行
是stderrlog的分支,stderrlog是一个旨在提供类似于env_logger的简单情况的logger,它只基于详细度记录到stderr。
文档
有关StructOpt、clap和docopt的工作示例,请参阅crate级文档。
有关显示如何使用模块级日志记录的示例二进制文件,请参阅large-example crate在examples/。
用法
将以下内容添加到您的Cargo.toml
[dependencies]
buche = "0.6"
并在您的main()中添加以下内容
buche::new().verbosity(args.flag_v).quiet(args.flag_q).init().unwrap();
其中您的args结构定义为
struct Args {
    flag_v: usize,
    flag_q: bool,
    ...
}
lib.rs:
一个简单的logger,通过将日志记录到stderr并提供更详细的详细度来提供类似于大多数UNIX工具期望的语义。它支持提供不同粒度的时间戳,以及为不同的日志级别着色。
简单用例
use log::*;
fn main() {
    buche::new().module(module_path!()).init().unwrap();
    error!("some failure");
    // ...
}
StructOpt示例
use log::*;
use structopt::StructOpt;
/// A StructOpt example
#[derive(StructOpt, Debug)]
#[structopt()]
struct Opt {
    /// Silence all output
    #[structopt(short = "q", long = "quiet")]
    quiet: bool,
    /// Verbose mode (-v, -vv, -vvv, etc)
    #[structopt(short = "v", long = "verbose", parse(from_occurrences))]
    verbose: usize,
    /// Timestamp (sec, ms, ns, none)
    #[structopt(short = "t", long = "timestamp")]
    ts: Option<buche::Timestamp>,
}
fn main() {
    let opt = Opt::from_args();
    buche::new()
        .module(module_path!())
        .quiet(opt.quiet)
        .verbosity(opt.verbose)
        .timestamp(opt.ts.unwrap_or(buche::Timestamp::Off))
        .init()
        .unwrap();
    trace!("trace message");
    debug!("debug message");
    info!("info message");
    warn!("warn message");
    error!("error message");
}
docopt示例
use log::*;
use docopt::Docopt;
use serde::Deserialize;
const USAGE: &'static str = "
Usage: program [-q] [-v...]
";
#[derive(Debug, Deserialize)]
struct Args {
    flag_q: bool,
    flag_v: usize,
}
fn main() {
    let args: Args = Docopt::new(USAGE)
                            .and_then(|d| d.deserialize())
                            .unwrap_or_else(|e| e.exit());
    buche::new()
            .module(module_path!())
            .quiet(args.flag_q)
            .timestamp(buche::Timestamp::Second)
            .verbosity(args.flag_v)
            .init()
            .unwrap();
    trace!("trace message");
    debug!("debug message");
    info!("info message");
    warn!("warn message");
    error!("error message");
    // ...
}
clap示例
use clap::{Arg, App, crate_version};
use log::*;
use std::str::FromStr;
fn main() {
    let m = App::new("buche example")
        .version(crate_version!())
        .arg(Arg::with_name("verbosity")
             .short('v')
             .takes_value(true)
             .multiple(true)
             .help("Increase message verbosity"))
        .arg(Arg::with_name("quiet")
             .short('q')
             .help("Silence all output"))
        .arg(Arg::with_name("timestamp")
             .short('t')
             .help("prepend log lines with a timestamp")
             .takes_value(true)
             .possible_values(&["none", "sec", "ms", "ns"]))
        .get_matches();
    let verbose = m.occurrences_of("verbosity") as usize;
    let quiet = m.is_present("quiet");
    let ts = m.value_of("timestamp").map(|v| {
        buche::Timestamp::from_str(v).unwrap_or_else(|_| {
            clap::Error::raw(clap::ErrorKind::InvalidValue, "invalid value for 'timestamp'").exit()
        })
    }).unwrap_or(buche::Timestamp::Off);
    buche::new()
        .module(module_path!())
        .quiet(quiet)
        .verbosity(verbose)
        .timestamp(ts)
        .init()
        .unwrap();
    trace!("trace message");
    debug!("debug message");
    info!("info message");
    warn!("warn message");
    error!("error message");
}
log兼容性
buche 0.5.x版本旨在与使用log >= 0.4.11的应用程序提供兼容性。
Rust兼容性
buche非常重视向后兼容性。buche在CI构建中固定了所需的最小Rust版本。提高Rust的最小版本是一个小的破坏性更改,需要将版本号提高一个小版本。
此版本的最低支持的Rust版本为1.36.0。
模块级日志记录
buche 具有限制可以记录日志的组件的能力。许多crate使用 log,但你可能不希望它们的应用程序中有输出。例如,hyper 严重依赖log,但当你的应用程序接收到 -vvvvv 以启用 trace!() 消息时,你不想看到 hyper 的 trace!() 级别的输出。
为了支持这一点,buche 包含了一个 module() 方法,允许你指定允许记录日志的模块。上面的例子使用了 module_path!() 宏来仅对二进制本身启用记录日志,而不包括其依赖项。要启用来自额外crate的日志记录,只需将具有crate名称的另一个调用添加到 module()。要只为该crate中的特定模块启用记录日志,请将 crate::module 传递给 module()。crate和模块将以与你在源代码中使用 use 包括它们相同的方式命名(例如,some-crate 将是 some_crate)。
要了解模块级日志记录的良好示例,请查看 examples 下的 large-example crate,你将想要运行以下二进制文件来查看所有示例
- cargorun --binlarge-example --
- cargorun --binanother --
- cargorun --binyet --
特性
buche 具有以下默认crate特性,可以通过禁用它们来减少依赖项的数量
- timestamps:提供对日志时间戳前缀的支持(使用- chronocrate)。
依赖关系
~0.2–7.5MB
~45K SLoC