Crate loggerv [] [src]

A simple io::stdout and io::stderr writing Logger implementation from the log crate, using the ansi_term crate for colors and configured at runtime via a verbosity or at compile time with simple function calls. Designed for simple Command Line Interfaces (CLIs).

This library includes a Builder pattern API for configuring a logger and three initializing helper functions to create a default logger. Ensure you create and initialize only once a global logger with the Builder pattern API or use one of the three public helper functions early in your program as shown in the examples below.

The default configuration colorizes the "tag" portion of the log statement, where the tag is the text to the left of a separator, defaulted as the colon (:). The message is the portion to the right of the separator and it is not ever colorized. The tag includes only the module path and the separator by default.

Example

The standard example with clap as the arg parser using the default configuration.

#[macro_use] extern crate log;
extern crate clap;
extern crate loggerv;

use clap::{Arg, App};

fn main() {
    let args = App::new("app")
        .arg(Arg::with_name("v")
            .short("v")
            .multiple(true)
            .help("Sets the level of verbosity"))
        .get_matches();

    loggerv::init_with_verbosity(args.occurrences_of("v")).unwrap();

    error!("This is always printed");
    warn!("This too is always printed to stderr");
    info!("This is optionally printed to stdout");  // for ./app -v or higher
    debug!("This is optionally printed to stdout"); // for ./app -vv or higher
    trace!("This is optionally printed to stdout"); // for ./app -vvv
}

But obviously use whatever argument parsing methods you prefer.

Example

For a compile time switch, all you really need is log (for the macros) and loggerv for how to print what's being sent to the macros with the default configuration.

#[macro_use] extern crate log;
extern crate loggerv;

use log::Level;

fn main() {
    loggerv::init_with_level(Level::Info).unwrap();
    debug!("This is a debug {}", "message"); // Not printed to stdout
    error!("This is printed by default");    // Printed to stderr
}

Example

If you don't really care at all you could just use the plain init_quiet function to only show warnings and errors with the default configuration:

#[macro_use] extern crate log;
extern crate loggerv;

fn main() {
    loggerv::init_quiet().unwrap();
    info!("Hidden");
    error!("This is printed by default");
}

Example

If you want to configure the output, the Builder pattern API can be used.

#[macro_use] extern crate log;
extern crate clap;
extern crate loggerv;

use clap::{Arg, App};

fn main() {
    let args = App::new("app")
                   .arg(Arg::with_name("v")
                            .short("v")
                            .multiple(true)
                            .help("Sets the level of verbosity"))
                   .get_matches();

    loggerv::Logger::new()
        .verbosity(args.occurrences_of("v"))
        .level(true)
        .line_numbers(true)
        .separator(" = ")
        .module_path(false)
        .colors(false)
        .init()
        .unwrap();

    error!("This is always printed to stderr");
    warn!("This too is always printed to stderr");
    info!("This is optionally printed to stdout");  // for ./app -v or higher
    debug!("This is optionally printed to stdout"); // for ./app -vv or higher
    trace!("This is optionally printed to stdout"); // for ./app -vvv
}

See the documentation for the log crate for more information about its API.

Structs

Logger

Enums

Output

Constants

DEFAULT_COLORS
DEFAULT_DEBUG_COLOR
DEFAULT_ERROR_COLOR
DEFAULT_INCLUDE_LEVEL
DEFAULT_INCLUDE_LINE_NUMBERS
DEFAULT_INCLUDE_MODULE_PATH
DEFAULT_INFO_COLOR
DEFAULT_LEVEL
DEFAULT_OFFSET
DEFAULT_SEPARATOR
DEFAULT_TRACE_COLOR
DEFAULT_WARN_COLOR

Functions

init_quiet

Initializes loggerv with only warnings and errors.

init_with_level

Initialize loggerv with a maximal log level.

init_with_verbosity

Initialize loggerv with a verbosity level.