5 stable releases

1.4.0 Jun 3, 2020
1.3.0 Jun 2, 2020
1.2.0 Jun 2, 2020
1.1.0 Apr 2, 2019
1.0.0 May 17, 2018

#475 in Debugging


Used in 3 crates (2 directly)

MIT license

18KB
147 lines

loggify

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

A small and simple log implementation that I use in my rust projects.

Install

Just add

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

to your Cargo.toml

Usage

All examples can be found in the examples directory.

Basic

The simpliest way is just to call init. The default log level is Info, so that debug and trace messages are not shown.

//! 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");
}

With log level

//! 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");
}

Log builder

//! 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");
}

Disable color output

You can either programmatically disable color output or use the env LOGGIFY_COLOR variable. To disable it using the env variable use LOGGIFY_COLOR=false.

If the value of the env variable cannot be parsed as a boolean value, the logger silently defaults to a colored output.

See the code example for programmatically disabling colored output.

//! 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");
}

Exclude targets from log

//! 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();
}

Example output

terminal

Dependencies

~1–1.5MB
~21K SLoC