#log #clap #env-logger

clap_logger

env_logger integration for clap

4 releases

Uses new Rust 2021

0.3.3 Aug 30, 2022
0.3.2 Aug 30, 2022
0.3.1 Apr 15, 2022
0.3.0 Apr 15, 2022

#2 in #env-logger

Download history 13/week @ 2022-06-06 4/week @ 2022-06-13 2/week @ 2022-06-20 3/week @ 2022-06-27 2/week @ 2022-07-04 4/week @ 2022-07-11 2/week @ 2022-07-18 8/week @ 2022-07-25 6/week @ 2022-08-01 1/week @ 2022-08-15 3/week @ 2022-08-22 37/week @ 2022-08-29 13/week @ 2022-09-05 5/week @ 2022-09-12 3/week @ 2022-09-19

58 downloads per month

AGPL-3.0

19KB
187 lines

clap_logger


lib.rs:

Clap Logger

Simple [env_logger][env_logger] integration for [clap][clap].

This create provides a simple way to allow the user to set the log level via a command line argument. Its directly implemented in clap, so it feels very naturally.

Please note this crate does not support clap_derive and won't support it in the near future or possibly never, since Integrating with it is very hard to do.

Features

  • Command line argument to set loglevel
  • Argument can be modified
  • Optional: Loglevel via Environment variables
  • directly embedded in clap::Command and clap::ArgMatches

Status: Beta

Finished

  • Feature complete (But Open for suggestions)
  • no panics

TODO

  • Waiting for feedback
  • more tests
  • Complete documentation
  • more examples

Backlog

  • Figure out if clap_derive support possible,

Adding the Argument

Base Implementation:

use clap::Command;
use log::LevelFilter;
use clap_logger::{ClapInitLogger, ClapLoglevelArg};

// Generate a clap command
let m: clap::ArgMatches = Command::new("clap_command_test")
  // add loglevel argument
   	.add_loglevel_arg(LevelFilter::Info)
	.get_matches();

loglevel Arg manipulation

You can also get the [Arg][clap::Arg] directly in order to modify it before adding:`

use clap::{arg, Arg, Command};
use log::LevelFilter;
use clap_logger::{ClapInitLogger, get_loglevel_arg};

// Generate a clap command
let m: clap::ArgMatches = Command::new("clap_command_test")
  // add the add loglevel argument
 	.arg(get_loglevel_arg(LevelFilter::Info)
		// Adding a short version
		.short('l')
    // changing the long version of the argument just because I can
		.long("custom-loglevel")
		.default_value("INFO")
  )
	.get_matches();

Warning: Do NOT touch .possible_values, .id field of the argument or anything in that modifies the input.

Initialising the logger

Base implementation:

use clap::Command;
use log::LevelFilter;
use clap_logger::{ClapInitLogger, ClapLoglevelArg};

let m: clap::ArgMatches = Command::new("clap_command_test")
  // add the loglevel argument
 	.add_loglevel_arg(LevelFilter::Info)
	.get_matches();

m.init_logger().expect("Failed to initialize logger");

Dependencies

~3.5MB
~72K SLoC