Expand description
A simple, opinionated logger for command line tools
ocli aims at a very simple thing: logging for CLI tools done right. It uses the
log crate and the ansi_term crate for colors. It provides very few configuration —
at this time, just the expected log level.
§Features
ocli:
- logs everything to
stderr. CLI tools are expected to be usable in a pipe. In that context, the messages addressed to the user must be written onstderrto have a chance to be read by the user, independently of the log level. The program outputs that are meant to be used with a pipe shouldn’t go through the logging system, but instead be printed tostdout, for example withprintln!. - shows the
Infomessage as plain uncolored text.Infois expected to be the normal log level to display messages that are not highlighting a problem and that are not too verbose for a standard usage of the tool. Because it is intended for messages that are related to a normal situation, the messages of that level are not prefixed with the log level. - prefix the messages with their colored log level for any level other than
Info. The color depends on the log level, allowing to quickly locate a message at a specific log level - displays the module path and line when configured at the
Tracelog level, for all the messages, even if they are not at theTracelog level. TheTracelog level is used to help the developer understand where a message comes from, in addition to display a larger amount of messages. - disables all colorization in case the
stderris not a tty, so the output is not polluted with unreadable characters whenstderris redirected to a file. This crates disables colorization when theNO_COLORenvironment is set, and force it whenFORCE_COLORis set. The colorization is disabled when both environment variables are set.
§Example with Info log level
#[macro_use] extern crate log;
fn main() {
ocli::init(log::Level::Info).unwrap();
error!("This is printed to stderr, with the 'error: ' prefix colored in red");
warn!("This is printed to stderr, with the 'warn: ' prefix colored in yellow");
info!("This is printed to stderr, without prefix or color");
debug!("This is not printed");
trace!("This is not printed");
}§Example with Trace log level
#[macro_use] extern crate log;
fn main() {
ocli::init(log::Level::Trace).unwrap();
error!("This is printed to stderr, with the 'path(line): error: ' prefix colored in red");
warn!("This is printed to stderr, with the 'path(line): warn: ' prefix colored in yellow");
info!(This is printed to stderr, with the 'path(line): info: ' prefix");
debug!("This is printed to stderr, with the 'path(line): debug: ' prefix colored in blue");
trace!(This is printed to stderr, with the 'path(line): trace: ' prefix colored in magenta");
}§Example with log level configured with a command line option
TODO: write a small example that uses clap derive
Structs§
Constants§
Functions§
- init
- Initializes the logger.