Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): Improve terminal styling support #298

Merged
merged 13 commits into from
Jan 19, 2024
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
msrv = "1.60.0" # MSRV
msrv = "1.71" # MSRV
warn-on-all-wildcard-imports = true
disallowed-methods = [
{ path = "std::option::Option::map_or", reason = "prefer `map(..).unwrap_or(..)` for legibility" },
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ jobs:
- name: Run crate example
run: cargo run --example default
msrv:
name: "Check MSRV: 1.60.0"
name: "Check MSRV: 1.71"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: "1.60" # MSRV
toolchain: "1.71" # MSRV
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@cargo-hack
- name: Check
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: "1.60" # MSRV
toolchain: "1.71" # MSRV
components: clippy
- uses: Swatinem/rust-cache@v2
- name: Install SARIF tools
Expand Down
195 changes: 67 additions & 128 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ categories = ["development-tools::debugging"]
keywords = ["logging", "log", "logger"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.71" # MSRV
include = [
"build.rs",
"src/**/*",
Expand Down Expand Up @@ -38,17 +38,17 @@ pre-release-replacements = [

[features]
default = ["auto-color", "humantime", "regex"]
color = ["dep:termcolor"]
auto-color = ["dep:is-terminal", "color"]
color = ["dep:anstream", "dep:anstyle"]
auto-color = ["color", "anstream/auto"]
humantime = ["dep:humantime"]
regex = ["dep:regex"]

[dependencies]
log = { version = "0.4.8", features = ["std"] }
regex = { version = "1.0.3", optional = true, default-features=false, features=["std", "perf"] }
termcolor = { version = "1.1.1", optional = true }
humantime = { version = "2.0.0", optional = true }
is-terminal = { version = "0.4.0", optional = true }
anstream = { version = "0.6.11", default-features = false, features = ["wincon"], optional = true }
anstyle = { version = "1.0.4", optional = true }

[[test]]
name = "regexp_filter"
Expand Down
15 changes: 8 additions & 7 deletions examples/custom_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you want to control the logging output completely, see the `custom_logger` ex

#[cfg(all(feature = "color", feature = "humantime"))]
fn main() {
use env_logger::{fmt::Color, Builder, Env};
use env_logger::{Builder, Env};

use std::io::Write;

Expand All @@ -30,16 +30,17 @@ fn main() {

Builder::from_env(env)
.format(|buf, record| {
let mut style = buf.style();
style.set_bg(Color::Yellow).set_bold(true);

// We are reusing `anstyle` but there are `anstyle-*` crates to adapt it to your
// preferred styling crate.
let warn_style = buf.default_level_style(log::Level::Warn);
let reset = warn_style.render_reset();
let warn_style = warn_style.render();
let timestamp = buf.timestamp();

writeln!(
buf,
"My formatted log ({}): {}",
timestamp,
style.value(record.args())
"My formatted log ({timestamp}): {warn_style}{}{reset}",
record.args()
)
})
.init();
Expand Down