1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#![doc(html_root_url = "https://docs.rs/sensible-env-logger/0.2")]
#![warn(rust_2018_idioms, missing_docs)]
#![deny(warnings, dead_code, unused_imports, unused_mut)]
//! [![github]](https://github.com/rnag/sensible-env-logger) [![crates-io]](https://crates.io/crates/sensible-env-logger) [![docs-rs]](https://docs.rs/sensible-env-logger)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
//!
//! <br>
//!
//! A simple logger, optionally configured via environment variables which
//! writes to standard error with nice colored output for log levels.
//! It sets up logging with "sensible" defaults that make it ideal for
//! running *[examples]* and *[tests]* on a crate of choice.
//!
//! [examples]: http://xion.io/post/code/rust-examples.html
//! [tests]: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
//! <br>
//!
//! ## Usage
//!
//! Even though it has `env` in the name, the `sensible-env-logger`
//! requires minimal configuration and setup to use:
//!
//! ```
//! #[macro_use] extern crate log;
//!
//! fn main() {
//! sensible_env_logger::init!();
//!
//! trace!("a trace example");
//! debug!("deboogging");
//! info!("such information");
//! warn!("o_O");
//! error!("boom");
//! }
//! ```
//!
//! Run the program and you should see all the log output for your crate.
//!
//! Alternatively, run the program with the environment variables that control
//! the log level for *your* crate as well as *external* crates explicitly set,
//! like `RUST_LOG=debug` and `GLOBAL_RUST_LOG=error`.
//!
//! ## Defaults
//!
//! The defaults can be setup by calling `init!()` or `try_init!()` at the start
//! of the program.
//!
//! ## Examples
//!
//! You can check out sample usage of this crate in the [examples/](https://github.com/rnag/sensible-env-logger/tree/main/examples)
//! folder in the project repo on GitHub.
//!
//! ## Readme Docs
//!
//! You can find the crate's readme documentation on the
//! [crates.io] page, or alternatively in the [`README.md`] file on the GitHub project repo.
//!
//! [crates.io]: https://crates.io/crates/sensible-env-logger
//! [`README.md`]: https://github.com/rnag/sensible-env-logger
//!
//! ## Enable logging
//!
//! This crate uses [pretty_env_logger] and [env_logger] internally, so the
//! same ways of enabling logs through an environment variable are supported.
//!
//! The `sensible_env_logger` crate re-exports these crates, through the
//! `pretty` and `env` namespaces respectively.
//!
//! [pretty_env_logger]: https://docs.rs/pretty_env_logger
//! [env_logger]: https://docs.rs/env_logger
#[cfg(feature = "local-time")]
pub use local_time::*;
#[doc(hidden)]
pub use pretty_env_logger as pretty;
#[doc(hidden)]
pub use pretty_env_logger::env_logger as env;
use std::borrow::Cow;
use env::Builder;
use log::{trace, SetLoggerError};
/// Default log level for the Cargo crate or package under test.
pub(crate) const CRATE_LOG_LEVEL: &str = "trace";
/// Default log level for external crates, other than the one under test.
pub(crate) const GLOBAL_LOG_LEVEL: &str = "warn";
/// Initializes the global logger with a pretty, sensible env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Panics
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! init {
() => {
$crate::try_init!().unwrap()
};
}
/// Initializes the global logger with a timed pretty, sensible env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Panics
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! init_timed {
() => {
$crate::try_init_timed!().unwrap();
};
}
/// Initializes the global logger with a pretty, sensible env logger.
///
/// This variant should ideally only be used in **tests**. It should be called
/// early in the execution of a Rust program.
///
/// Future initialization attempts will *safely ignore* any errors.
#[macro_export]
macro_rules! safe_init {
() => {
let _ = $crate::try_init!();
};
}
/// Initializes the global logger with a timed pretty, sensible env logger.
///
/// This variant should ideally only be used in **tests**. It should be called
/// early in the execution of a Rust program.
///
/// Future initialization attempts will *safely ignore* any errors.
#[macro_export]
macro_rules! safe_init_timed {
() => {
let _ = $crate::try_init_timed!();
};
}
/// Initializes the global logger with a pretty, sensible env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Errors
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! try_init {
() => {
$crate::try_init_custom_env_and_builder(
"RUST_LOG",
"GLOBAL_RUST_LOG",
env!("CARGO_PKG_NAME"),
module_path!(),
$crate::pretty::formatted_builder,
)
};
}
/// Initializes the global logger with a timed pretty, sensible env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Errors
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! try_init_timed {
() => {
$crate::try_init_custom_env_and_builder(
"RUST_LOG",
"GLOBAL_RUST_LOG",
env!("CARGO_PKG_NAME"),
module_path!(),
$crate::pretty::formatted_timed_builder,
)
};
}
/// Initializes the global logger with a pretty, sensible env logger, with custom
/// variable names and a custom builder function.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Example
/// ```rust
/// sensible_env_logger::try_init_custom_env_and_builder(
/// "MY_RUST_LOG",
/// "MY_GLOBAL_RUST_LOG",
/// env!("CARGO_PKG_NAME"),
/// module_path!(),
/// sensible_env_logger::pretty::formatted_timed_builder,
/// );
/// ```
///
/// # How It works
///
/// The `package_name` and `module_name` arguments are ideally evaluated from
/// the `$CARGO_PKG_NAME` and `$CARGO_CRATE_NAME` environment variables
/// respectively. These environment variables are automatically set
/// by Cargo when compiling your crate. It then builds a custom directives
/// string in the same form as the `$RUST_LOG` environment variable, and then
/// parses this generated directives string using
/// `env_logger::Builder::parse_filters`.
///
/// # Errors
///
/// This function fails to set the global logger if one has already been set.
pub fn try_init_custom_env_and_builder(
log_env_var: &str,
global_log_env_var: &str,
package_name: &str,
module_name: &str,
builder_fn: impl Fn() -> Builder,
) -> Result<(), SetLoggerError> {
let package_name = package_name.replace('-', "_");
let module_name = base_module(module_name);
let log_level = get_env(log_env_var, CRATE_LOG_LEVEL);
let global_log_level = get_env(global_log_env_var, GLOBAL_LOG_LEVEL);
let filters_str = if log_level.contains('=') {
// The env variable `$RUST_LOG` is set to a more complex value such as
// `warn,my_module=info`. In that case, just pass through the value.
log_level.into_owned()
} else if package_name != module_name {
format!(
"{default_lvl},{pkg}={lvl},{mod}={lvl}",
default_lvl = global_log_level,
pkg = package_name,
mod = module_name,
lvl = log_level
)
} else {
format!(
"{default_lvl},{pkg}={lvl}",
default_lvl = global_log_level,
pkg = package_name,
lvl = log_level
)
};
let mut builder: Builder = builder_fn();
builder.parse_filters(&filters_str);
let result = builder.try_init();
trace!("Filter: {}", filters_str);
result
}
/// Retrieve the value of an environment variable.
pub(crate) fn get_env<'a>(env_var_name: &'a str, default: &'a str) -> Cow<'a, str> {
match std::env::var(env_var_name) {
Ok(value) => Cow::Owned(value),
_ => Cow::Borrowed(default),
}
}
/// Returns the base module name, given the path to a module.
///
/// # Example
/// ```no_test
/// assert_eq!(base_module("my_bin::my_module::tests"), "my_bin");
/// ```
///
pub(crate) fn base_module(module_name: &str) -> &str {
match module_name.split_once("::") {
Some((first, _)) => first,
None => module_name,
}
}
#[cfg(feature = "local-time")]
mod local_time {
use super::*;
use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering};
use chrono::Local;
use env::fmt::{Color, Style, StyledValue};
use log::Level;
/// Initializes the global logger with an "abbreviated" timed pretty, sensible
/// env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Details
///
/// This variant formats log messages with a localized timestamp, without
/// the date part.
///
/// ## Example
///
/// ```console
/// 12:15:31.683 INFO my_module > an info message!
/// ```
///
/// # Requirements
///
/// Using this macro requires the `local-time` feature to be enabled:
///
/// ```toml
/// [dependencies]
/// sensible-env-logger = { version = "*", features = ["local-time"] }
/// ```
///
/// # Panics
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! init_timed_short {
() => {
$crate::try_init_timed_short!().unwrap();
};
}
/// Initializes the global logger with an "abbreviated" timed pretty, sensible
/// env logger. See [`init_timed_short`] for more info.
///
/// This variant should ideally only be used in **tests**. It should be called
/// early in the execution of a Rust program.
///
/// Future initialization attempts will *safely ignore* any errors.
#[macro_export]
macro_rules! safe_init_timed_short {
() => {
let _ = $crate::try_init_timed_short!();
};
}
/// Initializes the global logger with an "abbreviated" timed pretty, sensible
/// env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Details
///
/// This variant formats log messages with a localized timestamp, without
/// the date part.
///
/// ## Example
/// ```console
/// 12:15:31.683 INFO my_module > an info message!
/// ```
///
/// # Requirements
///
/// Using this macro requires the `local-time` feature to be enabled:
///
/// ```toml
/// [dependencies]
/// sensible-env-logger = { version = "*", features = ["local-time"] }
/// ```
///
/// # Errors
///
/// This macro fails to set the global logger if one has already been set.
#[macro_export]
macro_rules! try_init_timed_short {
() => {
$crate::try_init_custom_env_and_builder(
"RUST_LOG",
"GLOBAL_RUST_LOG",
env!("CARGO_PKG_NAME"),
module_path!(),
$crate::formatted_short_timed_builder,
)
};
}
/// Returns a formatted builder which adds local time to log messages.
///
/// ## Example
/// ```console
/// 12:15:31.683 INFO my_module > an info message!
/// ```
///
pub fn formatted_short_timed_builder() -> Builder {
let mut builder = Builder::new();
builder.format(|f, record| {
use std::io::Write;
let target = record.target();
let max_width = max_target_width(target);
let mut style = f.style();
let level = colored_level(&mut style, record.level());
let mut style = f.style();
let target = style.set_bold(true).value(Padded {
value: target,
width: max_width,
});
let time = Local::now().format("%l:%M:%S.%3f");
writeln!(f, " {} {} {} > {}", time, level, target, record.args(),)
});
builder
}
/// Helper functions
///
/// Below are copied verbatim from [`pretty_env_logger`]
///
/// [`pretty_env_logger`]: https://github.com/seanmonstar/pretty-env-logger/blob/master/src/lib.rs
///
struct Padded<T> {
value: T,
width: usize,
}
impl<T: fmt::Display> fmt::Display for Padded<T> {
fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result {
write!(f, "{: <width$}", self.value, width = self.width)
}
}
static MAX_MODULE_WIDTH: AtomicUsize = AtomicUsize::new(0);
fn max_target_width(target: &str) -> usize {
let max_width = MAX_MODULE_WIDTH.load(Ordering::Relaxed);
if max_width < target.len() {
MAX_MODULE_WIDTH.store(target.len(), Ordering::Relaxed);
target.len()
} else {
max_width
}
}
fn colored_level<'a>(style: &'a mut Style, level: Level) -> StyledValue<'a, &'static str> {
match level {
Level::Trace => style.set_color(Color::Magenta).value("TRACE"),
Level::Debug => style.set_color(Color::Blue).value("DEBUG"),
Level::Info => style.set_color(Color::Green).value("INFO "),
Level::Warn => style.set_color(Color::Yellow).value("WARN "),
Level::Error => style.set_color(Color::Red).value("ERROR"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use log::*;
#[test]
fn logging_in_tests() {
// Initialize the global logger with sensible defaults
init!();
trace!("A simple trace message");
debug!("Debugging something...");
warn!("This is a WARNING!");
}
#[test]
fn test_base_module_simple() {
let result = base_module("hello_world");
assert_eq!(result, "hello_world");
}
#[test]
fn test_base_module_with_nested_path() {
let result = base_module("my_bin::my_module::tests");
assert_eq!(result, "my_bin");
}
}