5个稳定版本

1.4.0 2020年6月3日
1.3.0 2020年6月2日
1.2.0 2020年6月2日
1.1.0 2019年4月2日
1.0.0 2018年5月17日

调试分类中排名第367

Download history 4/week @ 2024-06-29 54/week @ 2024-07-27

每月下载量58
3个crate(其中2个直接使用)使用

MIT许可证

18KB
147 代码行

loggify

conduct-badge release-badge docs-badge license-badge

一个小型简单的日志实现,我在我的Rust项目中使用。

安装

只需添加

[dependencies]
log = "0.4.8"
loggify = "1"

到您的Cargo.toml

使用方法

所有示例都可以在examples目录中找到。

基本用法

最简单的方法是调用init。默认日志级别是Info,因此不会显示调试和跟踪消息。

//! examples/01_basic.rs
//! # Basic usage for the logger
use log::{error, warn, info, debug, trace};
use loggify::Loggify;

/// The default level is INFO. So debug and trace outputs are oppressed
fn main() {
    Loggify::init().unwrap();

    error!("My error message");
    warn!("My warn message");
    info!("My info message");
    debug!("Will not be shown");
    trace!("Will not be shown");
}

带有日志级别

//! examples/02_log_level.rs
//! Example for initializing the logger with a log level
use log::{error, warn, info, debug, trace, Level};
use loggify::Loggify;

/// Same as the basic example with the difference that
/// the logger is intialized with the debug log level.
fn main() {
    Loggify::init_with_level(Level::Debug).unwrap();

    error!("My error message");
    warn!("My warn message");
    info!("My info message");
    debug!("My debug message");
    trace!("Will not be shown");
}

日志构建器

//! examples/03_builder.rs
//! Example for initializing the logger with the LogBuilder
use log::{error, warn, info, debug, trace};
use loggify::LogBuilder;

/// The `LogBuilder` is used to set more logger options
/// This example will change the log level to Trace
/// and the printed time format to time only
fn main() {
    LogBuilder::new()
        .set_level(log::Level::Trace)
        .set_time_format(String::from("%H:%M:%S"))
        .build()
        .unwrap();

    error!("My error message");
    warn!("My warn message");
    info!("My info message");
    debug!("My debug message");
    trace!("My trace message");
}

禁用彩色输出

您可以通过编程方式禁用彩色输出或使用环境变量LOGGIFY_COLOR。要使用环境变量禁用它,请使用LOGGIFY_COLOR=false

如果环境变量的值无法解析为布尔值,则记录器默认为彩色输出。

请参阅禁用彩色输出的代码示例。

//! examples/05_builder.rs
//! Example for initializing the logger with the LogBuilder
use log::{error, warn, info, debug, trace};
use loggify::LogBuilder;

/// The `LogBuilder` is used to set more logger options
/// This example will disable the colored output
fn main() {
    LogBuilder::new()
        .disable_color()
        .build()
        .unwrap();

    error!("My error message");
    warn!("My warn message");
    info!("My info message");
    debug!("My debug message");
    trace!("My trace message");
}

排除日志中的目标

//! examples/04_exclude.rs
//! Example for excluding log targets from getting logged
use log::{error, warn, info, debug, trace};
use loggify::LogBuilder;

mod example {
    pub mod excluded {
        use log::info;

        pub fn call_me() {
            info!("I will not be logged");
        }
    }

    pub mod included {
        use log::info;

        pub fn call_me() {
            info!("I will be logged");
        }
    }
}

/// Exmple on how to exclude specific log targets
fn main() {
    LogBuilder::new()
        // this will show the log targets so that we can determine
        // what to exclude
        .set_log_target(true)
        // this will oppress all logs coming from example::excluded::*
        .add_exclude("example::excluded".to_string())
        .set_level(log::Level::Trace)
        .build()
        .unwrap();

    error!("My error message");
    warn!("My warn message");
    info!("My info message");
    debug!("My debug message");
    trace!("My trace message");

    // the log message of this call will not be shown
    example::excluded::call_me();
    // this log message will be shown
    example::included::call_me();
}

示例输出

terminal

依赖关系

~1–1.5MB
~20K SLoC