Public color API#46
Conversation
KodrAus
left a comment
There was a problem hiding this comment.
Needs some more work to smooth out the rough edges.
| let mut builder = Builder::new(); | ||
| let Env { filter, write_style } = env.into(); | ||
|
|
||
| if let Ok(s) = env::var(&*filter) { |
There was a problem hiding this comment.
Repeating this check is a bit nasty. I think we should add methods to Env to get an Option for each variable
|
|
||
| if tl_buf.is_none() { | ||
| *tl_buf = Some(Formatter::new(self.writer.buffer())); | ||
| *tl_buf = Some(Formatter::new(self.writer.buffer(), self.write_style)); |
There was a problem hiding this comment.
This is problematic because multiple loggers working on the same thread with different configurations will get unexpected results. The problem is the same with the Buffer, since it's tied to the Logger.
I'm thinking we just prevent you building a Logger directly and require it's configured globally, but am open to other suggestions.
|
Looks like we could handle the colors better by using |
|
We should align the values of |
|
I've done some refactoring so all the code around writing to the terminal is contained in the I think this will make the library easier to navigate and use in the future since things are getting more complex in |
| Also try setting the `MY_LOG_STYLE` environment variable to `0` to disable colors: | ||
|
|
||
| ```no_run,shell | ||
| $ export MY_LOG_STYLE=0 |
There was a problem hiding this comment.
This value is wrong. It should be auto, always or never.
| Also try setting the `RUST_LOG_STYLE` environment variable to `0` to disable colors: | ||
|
|
||
| ```no_run,shell | ||
| $ export RUST_LOG_STYLE=0 |
There was a problem hiding this comment.
Same as other style examples.
| Also try setting the `RUST_LOG_STYLE` environment variable to `0` to disable colors: | ||
|
|
||
| ```no_run,shell | ||
| $ export RUST_LOG_STYLE=0 |
There was a problem hiding this comment.
Same as other style examples.
| } | ||
| } | ||
|
|
||
| pub(crate) struct Writer(BufferWriter); |
There was a problem hiding this comment.
Even though these are private right now we should add some docs to them.
Closes #45 and #42
Some thoughts
Envbe an&mut selfbuilder like the others?StyleimplementWriteso you can callwriteln!(read_style, "{}", record.args())to write an entire line using styles?Refactorings
I've moved all the terminal writing related code, like
Targetand the newWriteStyleinto thefmtmodule to try and encapsulate it a bit better. Now we don't depend ontermcolordirectly in theLoggerorBuilder.Disabling colors
Adds a new builder property for whether or not to include colors. By default this is
RUST_LOG_STYLE, but it can be overridden.Setting this environment variable to
neverwill disable colors and other styles:$ export RUST_LOG_STYLE=never $ ./my-appValid values are:
auto(or missing/invalid) will decide whether or not the terminal supports colorsalwayswill always use colorsneverwill never use colorsIn order to support multiple environment variables, I've refactored our
from_envfunctions to accept a genericT: Into<Env>, whereEnvis a container for the environment variables we care about. These methods can now be called in a few ways: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_envmethods. This PR adds anEnvtype with builder methods for naming environment variables:With a few
Fromconversions, the above is also equivalent to:Whether or not we want to keep these conversions is up for discussion.
Writing colors
The color API has been refactored so it's a bit nicer to use in your own formats:
This saves you from having to split the writes into multiple calls and juggle
Resulttypes.Writing timestamps
Oh the
Formatter.timestampmethod is now also public so you can grab timestamps.