Skip to content

Commit 522ce17

Browse files
authored
Merge pull request #310 from epage/docs
docs: Talk abotu new styling API
2 parents 8c94cd5 + c67579c commit 522ce17

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515

1616
## [0.11.0] - 2024-01-19
1717

18+
### Migration Guide
19+
20+
**env_logger::fmt::Style:**
21+
The bespoke styling API, behind `color`, was removed, in favor of accepting any
22+
ANSI styled string and adapting it to the target stream's capabilities.
23+
24+
Possible styling libraries include:
25+
- [anstyle](https://docs.rs/anstyle) is a minimal, runtime string styling API and is re-exported as `env_logger::fmt::style`
26+
- [owo-colors](https://docs.rs/owo-colors) is a feature rich runtime string styling API
27+
- [color-print](https://docs.rs/color-print) for feature-rich compile-time styling API
28+
29+
[custom_format.rs](https://docs.rs/env_logger/latest/src/custom_format/custom_format.rs.html)
30+
uses `anstyle` via
31+
[`Formatter::default_level_style`](https://docs.rs/env_logger/latest/env_logger/fmt/struct.Formatter.html#method.default_level_style)
32+
1833
### Breaking Change
1934

2035
- Removed bespoke styling API

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ log = { version = "0.4.8", features = ["std"] }
5858
env_filter = { version = "0.1.0", path = "crates/env_filter", default-features = false }
5959
humantime = { version = "2.0.0", optional = true }
6060
anstream = { version = "0.6.11", default-features = false, features = ["wincon"], optional = true }
61-
anstyle = { version = "1.0.4", optional = true }
61+
anstyle = { version = "1.0.6", optional = true }
6262

6363
[[test]]
6464
name = "regexp_filter"

examples/custom_format.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ fn main() {
3333
// We are reusing `anstyle` but there are `anstyle-*` crates to adapt it to your
3434
// preferred styling crate.
3535
let warn_style = buf.default_level_style(log::Level::Warn);
36-
let reset = warn_style.render_reset();
37-
let warn_style = warn_style.render();
3836
let timestamp = buf.timestamp();
3937

4038
writeln!(
4139
buf,
42-
"My formatted log ({timestamp}): {warn_style}{}{reset}",
40+
"My formatted log ({timestamp}): {warn_style}{}{warn_style:#}",
4341
record.args()
4442
)
4543
})

src/fmt/humantime.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ impl Formatter {
2525
/// writeln!(buf, "{}: {}: {}", ts, record.level(), record.args())
2626
/// });
2727
/// ```
28-
///
29-
/// [`Timestamp`]: struct.Timestamp.html
3028
pub fn timestamp(&self) -> Timestamp {
3129
Timestamp {
3230
time: SystemTime::now(),
@@ -76,8 +74,7 @@ impl Formatter {
7674
/// The timestamp implements [`Display`] and can be written to a [`Formatter`].
7775
///
7876
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
79-
/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
80-
/// [`Formatter`]: struct.Formatter.html
77+
/// [`Display`]: std::fmt::Display
8178
pub struct Timestamp {
8279
time: SystemTime,
8380
precision: TimestampPrecision,

src/fmt/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
//!
1010
//! The format used to print log records can be customised using the [`Builder::format`]
1111
//! method.
12-
//! Custom formats can apply different color and weight to printed values using
13-
//! [`Style`] builders.
12+
//!
13+
//! Terminal styling is done through ANSI escape codes and will be adapted to the capabilities of
14+
//! the target stream.
15+
//! For example, you could use one of:
16+
//! - [anstyle](https://docs.rs/anstyle) is a minimal, runtime string styling API and is re-exported as [`style`]
17+
//! - [owo-colors](https://docs.rs/owo-colors) is a feature rich runtime string styling API
18+
//! - [color-print](https://docs.rs/color-print) for feature-rich compile-time styling API
19+
//! See also [`Formatter::default_level_style`]
1420
//!
1521
//! ```
1622
//! use std::io::Write;
@@ -24,10 +30,8 @@
2430
//! });
2531
//! ```
2632
//!
27-
//! [`Formatter`]: struct.Formatter.html
28-
//! [`Style`]: struct.Style.html
29-
//! [`Builder::format`]: ../struct.Builder.html#method.format
30-
//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
33+
//! [`Builder::format`]: crate::Builder::format
34+
//! [`Write`]: std::io::Write
3135
3236
use std::cell::RefCell;
3337
use std::fmt::Display;
@@ -80,7 +84,7 @@ impl Default for TimestampPrecision {
8084
/// A formatter to write logs into.
8185
///
8286
/// `Formatter` implements the standard [`Write`] trait for writing log records.
83-
/// It also supports terminal colors, through the [`style`] method.
87+
/// It also supports terminal styling using ANSI escape codes.
8488
///
8589
/// # Examples
8690
///
@@ -95,9 +99,8 @@ impl Default for TimestampPrecision {
9599
/// builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()));
96100
/// ```
97101
///
98-
/// [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
99-
/// [`writeln`]: https://doc.rust-lang.org/stable/std/macro.writeln.html
100-
/// [`style`]: #method.style
102+
/// [`Write`]: std::io::Write
103+
/// [`writeln`]: std::writeln
101104
pub struct Formatter {
102105
buf: Rc<RefCell<Buffer>>,
103106
write_style: WriteStyle,
@@ -129,6 +132,8 @@ impl Formatter {
129132
/// Get the default [`style::Style`] for the given level.
130133
///
131134
/// The style can be used to print other values besides the level.
135+
///
136+
/// See [`style`] for how to adapt it to the styling crate of your choice
132137
pub fn default_level_style(&self, level: Level) -> style::Style {
133138
if self.write_style == WriteStyle::Never {
134139
style::Style::new()
@@ -238,10 +243,6 @@ type SubtleStyle = StyledValue<&'static str>;
238243
type SubtleStyle = &'static str;
239244

240245
/// A value that can be printed using the given styles.
241-
///
242-
/// It is the result of calling [`Style::value`].
243-
///
244-
/// [`Style::value`]: struct.Style.html#method.value
245246
#[cfg(feature = "color")]
246247
struct StyledValue<T> {
247248
style: style::Style,
@@ -251,14 +252,13 @@ struct StyledValue<T> {
251252
#[cfg(feature = "color")]
252253
impl<T: std::fmt::Display> std::fmt::Display for StyledValue<T> {
253254
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
254-
let style = self.style.render();
255-
let reset = self.style.render_reset();
255+
let style = self.style;
256256

257257
// We need to make sure `f`s settings don't get passed onto the styling but do get passed
258258
// to the value
259259
write!(f, "{style}")?;
260260
self.value.fmt(f)?;
261-
write!(f, "{reset}")?;
261+
write!(f, "{style:#}")?;
262262
Ok(())
263263
}
264264
}

src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,6 @@
259259
//! [gh-repo-examples]: https://github.com/rust-cli/env_logger/tree/main/examples
260260
//! [level-enum]: https://docs.rs/log/latest/log/enum.Level.html
261261
//! [log-crate-url]: https://docs.rs/log
262-
//! [`Builder`]: struct.Builder.html
263-
//! [`Builder::is_test`]: struct.Builder.html#method.is_test
264-
//! [`Env`]: struct.Env.html
265-
//! [`fmt`]: fmt/index.html
266262
267263
#![doc(
268264
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",

0 commit comments

Comments
 (0)