diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 4b6110b3..a135b860 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -3,7 +3,7 @@ name: Continuous Integration - Docs on: push: branches: - - master + - main paths: - "**.rs" - "Cargo.toml" diff --git a/Cargo.toml b/Cargo.toml index 29c77b3b..aca247bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,8 @@ [package] name = "env_logger" edition = "2018" -version = "0.9.0" -authors = ["The Rust Project Developers"] -license = "MIT/Apache-2.0" +version = "0.9.2" +license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/env-logger-rs/env_logger/" documentation = "https://docs.rs/env_logger" @@ -23,7 +22,7 @@ members = [ [dependencies] log = { version = "0.4.8", features = ["std"] } regex = { version = "1.0.3", optional = true, default-features=false, features=["std", "perf"] } -termcolor = { version = "1.0.2", optional = true } +termcolor = { version = "1.1.1", optional = true } humantime = { version = "2.0.0", optional = true } atty = { version = "0.2.5", optional = true } diff --git a/LICENSE-MIT b/LICENSE-MIT index 39d4bdb5..31aa7938 100644 --- a/LICENSE-MIT +++ b/LICENSE-MIT @@ -1,5 +1,3 @@ -Copyright (c) 2014 The Rust Project Developers - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the diff --git a/README.md b/README.md index 4e84e8f3..75add88f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # env_logger -[![Maintenance](https://img.shields.io/badge/maintenance-actively%20maintained-brightgreen.svg)](https://github.com/env-logger-rs/env_logger) [![crates.io](https://img.shields.io/crates/v/env_logger.svg)](https://crates.io/crates/env_logger) [![Documentation](https://docs.rs/env_logger/badge.svg)](https://docs.rs/env_logger) -[![Documentation](https://img.shields.io/badge/docs-master-blue.svg)](https://env-logger-rs.github.io/env_logger/env_logger/index.html) -========== +[![Documentation](https://img.shields.io/badge/docs-main-blue.svg)](https://env-logger-rs.github.io/env_logger/env_logger/index.html) Implements a logger that can be configured via environment variables. @@ -12,7 +10,7 @@ Implements a logger that can be configured via environment variables. ### In libraries -`env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://doc.rust-lang.org/log) crate instead. +`env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://docs.rs/log) crate instead. ### In executables @@ -21,7 +19,7 @@ It must be added along with `log` to the project dependencies: ```toml [dependencies] log = "0.4.0" -env_logger = "0.8.4" +env_logger = "0.9.0" ``` `env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging. @@ -77,7 +75,7 @@ There is also a pseudo logging level, `off`, which may be specified to disable all logging for a given module or for the entire application. As with the logging levels, the letter case is not significant. -`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/env-logger-rs/env_logger/tree/master/examples) for more approaches. +`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/env-logger-rs/env_logger/tree/main/examples) for more approaches. ### In tests @@ -88,7 +86,7 @@ Tests can use the `env_logger` crate to see log messages generated during that t log = "0.4.0" [dev-dependencies] -env_logger = "0.8.4" +env_logger = "0.9.0" ``` ```rust diff --git a/ci/Cargo.toml b/ci/Cargo.toml index 87258b6b..a2c8df58 100644 --- a/ci/Cargo.toml +++ b/ci/Cargo.toml @@ -2,7 +2,6 @@ name = "ci" edition = "2018" version = "0.0.0" -authors = ["The Rust Project Developers"] publish = false [dependencies] diff --git a/ci/src/task.rs b/ci/src/task.rs index 85f7ce63..ed55bdda 100644 --- a/ci/src/task.rs +++ b/ci/src/task.rs @@ -27,7 +27,7 @@ impl TestArgs { let s = self.features.iter().fold(String::new(), |mut s, f| { if !s.is_empty() { - s.push_str(" "); + s.push(' '); } s.push_str(f); @@ -58,7 +58,7 @@ pub fn test(args: TestArgs) -> bool { } if let Some(ref features) = features { - command.args(&["--features", features]); + command.args(["--features", features]); } println!("running {:?}", command); diff --git a/examples/custom_target.rs b/examples/custom_target.rs deleted file mode 100644 index 384d244a..00000000 --- a/examples/custom_target.rs +++ /dev/null @@ -1,81 +0,0 @@ -/*! -Using `env_logger`. - -Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`: - -```no_run,shell -$ export MY_LOG_LEVEL='info' -``` - -Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors -or `auto` to enable them: - -```no_run,shell -$ export MY_LOG_STYLE=never -``` -*/ - -#[macro_use] -extern crate log; - -use env_logger::{Builder, Env, Target}; -use std::{ - io, - sync::mpsc::{channel, Sender}, -}; - -// This struct is used as an adaptor, it implements io::Write and forwards the buffer to a mpsc::Sender -struct WriteAdapter { - sender: Sender, -} - -impl io::Write for WriteAdapter { - // On write we forward each u8 of the buffer to the sender and return the length of the buffer - fn write(&mut self, buf: &[u8]) -> io::Result { - for chr in buf { - self.sender.send(*chr).unwrap(); - } - Ok(buf.len()) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - -fn main() { - // The `Env` lets us tweak what the environment - // variables to read are and what the default - // value is if they're missing - let env = Env::default() - .filter_or("MY_LOG_LEVEL", "trace") - // Normally using a pipe as a target would mean a value of false, but this forces it to be true. - .write_style_or("MY_LOG_STYLE", "always"); - - // Create the channel for the log messages - let (rx, tx) = channel(); - - Builder::from_env(env) - // The Sender of the channel is given to the logger - // A wrapper is needed, because the `Sender` itself doesn't implement `std::io::Write`. - .target(Target::Pipe(Box::new(WriteAdapter { sender: rx }))) - .init(); - - trace!("some trace log"); - debug!("some debug log"); - info!("some information log"); - warn!("some warning log"); - error!("some error log"); - - // Collect all messages send to the channel and parse the result as a string - String::from_utf8(tx.try_iter().collect::>()) - .unwrap() - // Split the result into lines so a prefix can be added to each line - .split('\n') - .for_each(|msg| { - // Print the message with a prefix if it has any content - if !msg.is_empty() { - println!("from pipe: {}", msg) - } - }); -} diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 9ebeab0b..a02d6bc1 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -151,7 +151,7 @@ impl Filter { } if let Some(filter) = self.filter.as_ref() { - if !filter.is_match(&*record.args().to_string()) { + if !filter.is_match(&record.args().to_string()) { return false; } } diff --git a/src/fmt/humantime/extern_impl.rs b/src/fmt/humantime/extern_impl.rs index 19dec1b6..bdf165c4 100644 --- a/src/fmt/humantime/extern_impl.rs +++ b/src/fmt/humantime/extern_impl.rs @@ -99,7 +99,7 @@ impl fmt::Debug for Timestamp { } f.debug_tuple("Timestamp") - .field(&TimestampValue(&self)) + .field(&TimestampValue(self)) .finish() } } diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index 21e09577..1677887a 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -406,7 +406,7 @@ mod tests { String::from_utf8(buf.bytes().to_vec()).expect("failed to read record") } - fn write_target<'a>(target: &'a str, fmt: DefaultFormat) -> String { + fn write_target(target: &str, fmt: DefaultFormat) -> String { write_record( Record::builder() .args(format_args!("log\nmessage")) diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index 5bb53539..2b56772e 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -193,7 +193,7 @@ impl Builder { let writer = match mem::take(&mut self.target) { WritableTarget::Stderr => BufferWriter::stderr(self.is_test, color_choice), WritableTarget::Stdout => BufferWriter::stdout(self.is_test, color_choice), - WritableTarget::Pipe(pipe) => BufferWriter::pipe(self.is_test, color_choice, pipe), + WritableTarget::Pipe(pipe) => BufferWriter::pipe(color_choice, pipe), }; Writer { diff --git a/src/fmt/writer/termcolor/extern_impl.rs b/src/fmt/writer/termcolor/extern_impl.rs index 11012fb1..fbe37a77 100644 --- a/src/fmt/writer/termcolor/extern_impl.rs +++ b/src/fmt/writer/termcolor/extern_impl.rs @@ -71,19 +71,19 @@ impl Formatter { pub(in crate::fmt::writer) struct BufferWriter { inner: termcolor::BufferWriter, - test_target: Option, + uncolored_target: Option, } pub(in crate::fmt) struct Buffer { inner: termcolor::Buffer, - has_test_target: bool, + has_uncolored_target: bool, } impl BufferWriter { pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()), - test_target: if is_test { + uncolored_target: if is_test { Some(WritableTarget::Stderr) } else { None @@ -94,7 +94,7 @@ impl BufferWriter { pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()), - test_target: if is_test { + uncolored_target: if is_test { Some(WritableTarget::Stdout) } else { None @@ -103,30 +103,25 @@ impl BufferWriter { } pub(in crate::fmt::writer) fn pipe( - is_test: bool, write_style: WriteStyle, pipe: Box>, ) -> Self { BufferWriter { // The inner Buffer is never printed from, but it is still needed to handle coloring and other formating inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()), - test_target: if is_test { - Some(WritableTarget::Pipe(pipe)) - } else { - None - }, + uncolored_target: Some(WritableTarget::Pipe(pipe)), } } pub(in crate::fmt::writer) fn buffer(&self) -> Buffer { Buffer { inner: self.inner.buffer(), - has_test_target: self.test_target.is_some(), + has_uncolored_target: self.uncolored_target.is_some(), } } pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { - if let Some(target) = &self.test_target { + if let Some(target) = &self.uncolored_target { // This impl uses the `eprint` and `print` macros // instead of `termcolor`'s buffer. // This is so their output can be captured by `cargo test` @@ -164,7 +159,7 @@ impl Buffer { fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { // Ignore styles for test captured logs because they can't be printed - if !self.has_test_target { + if !self.has_uncolored_target { self.inner.set_color(spec) } else { Ok(()) @@ -173,7 +168,7 @@ impl Buffer { fn reset(&mut self) -> io::Result<()> { // Ignore styles for test captured logs because they can't be printed - if !self.has_test_target { + if !self.has_uncolored_target { self.inner.reset() } else { Ok(()) @@ -339,6 +334,33 @@ impl Style { self } + /// Set whether the text is dimmed. + /// + /// If `yes` is true then text will be written in a dimmer color. + /// If `yes` is false then text will be written in the default color. + /// + /// # Examples + /// + /// Create a style with dimmed text: + /// + /// ``` + /// use std::io::Write; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_dimmed(true); + /// + /// writeln!(buf, "{}", style.value(record.args())) + /// }); + /// ``` + pub fn set_dimmed(&mut self, yes: bool) -> &mut Style { + self.spec.set_dimmed(yes); + self + } + /// Set the background color. /// /// # Examples @@ -453,7 +475,7 @@ impl_styled_value_fmt!( fmt::LowerExp ); -// The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor +// The `Color` type is copied from https://github.com/BurntSushi/termcolor /// The set of available colors for the terminal foreground/background. /// diff --git a/src/lib.rs b/src/lib.rs index 85041085..642b731a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,6 @@ -// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// // Licensed under the Apache License, Version 2.0 or the MIT license -// , at your +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// , at your // option. This file may not be copied, modified, or distributed // except according to those terms. @@ -107,7 +103,7 @@ //! your program is in a file called, for example, `hello.rs`, the path would //! simply be be `hello`. //! -//! Furthermore, the the log can be filtered using prefix-search based on the +//! Furthermore, the log can be filtered using prefix-search based on the //! specified log target. A value of, for example, `RUST_LOG=example`, would //! match all of the messages with targets: //! @@ -676,7 +672,7 @@ impl Builder { /// /// # Examples /// - /// Only include messages for info and above for logs in `path::to::module`: + /// Only include messages for info and above for logs globally: /// /// ``` /// use env_logger::Builder;