Skip to content

0.5.0

Compare
Choose a tag to compare
@KodrAus KodrAus released this 17 Jan 21:19
· 469 commits to main since this release
1908978

NOTE: These release notes are collected from the 0.5.0-rc.1 and 0.5.0-rc.2 release notes.

Key Changes

  • Support for using custom environment variables to parse the log level filter and color output
  • Added colors with configuration through an environment variable and an RFC3339-formatted timestamp to the default format
  • Buffer log output so logs over multiple lines won't end up interleaved in multi-threaded applications
  • Move the filter parsing into its own module so it's easier to consume in custom loggers
  • Documentation!

Breaking Changes

  • LogTarget has been renamed to Target
  • LogBuilder has been renamed to Builder
  • The Builder::format method now accepts a F: Fn(&mut Formatter, &Record) -> Result<()> + Sync + Send. This is the new formatting API that writes into a Formatter instead of producing an owned String
  • Builder::init will panic if the logger can't be initialised. A new Builder::try_init method has been added with the same semantics as the old Builder::init method

New Dependencies

Contributions

Thanks to everybody who helped make this release of env_logger happen!

More Details

Disabling colors

Adds a new builder property for whether or not to include colors. By default this is controlled by the RUST_LOG_STYLE environment variable, but it can be overridden.

Setting this environment variable to never will disable colors and other styles:

$ export RUST_LOG_STYLE=never
$ ./my-app

Valid values are:

  • auto (or missing/invalid) will decide whether or not the terminal supports colors
  • always will always use colors
  • never will never use colors

In order to support multiple environment variables, I've refactored our from_env functions to accept a generic T: Into<Env>, where Env is a container for the environment variables we care about. These methods can now be called in a few ways:

// reads filters from `MY_LOG` and styles from `RUST_LOG_STYLE`
env_logger::init_from_env("MY_LOG");

// reads filters from `MY_LOG` and styles from `MY_LOG_STYLE`
env_logger::init_from_env(Env::default().filter("MY_LOG").write_style("MY_LOG_STYLE"));

This lets us add new environment variables in the future without potentially breaking people. But it does mean if you're overriding all environment variables that new ones could slip in without you noticing.

Using alternative environment variables

Since we use two environment variables to configure the logger we need an ergonomic way to pass different combinations of those variables to from_env methods. This PR adds an Env type with builder methods for naming environment variables:

env_logger::init_from_env(Env::new().filter("MY_LOG"));

With a few From conversions, the above is also equivalent to:

env_logger::init_from_env("MY_LOG");

Whether or not we want to keep these conversions is up for discussion.

Writing colors

The color API has been refactored and made public so you can use them in your own formats:

let mut style = buf.style();

style.set_color(Color::Red).set_bold(true).set_bg(Color::White);

writeln!(buf, "{}", style.value(42))

This saves you from having to split the writes into multiple calls and juggle Result types.

Writing timestamps

Call the timestamp method on a Formatter to get an opaque timestamp that can be logged. It'll be written in an RFC3339 format:

let ts = buf.timestamp();

writeline!(buf, "log at: {}", ts)