-
Notifications
You must be signed in to change notification settings - Fork 729
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
fmt: support customizing output stream #299
Conversation
- Add a new trait, `NewWriter`, that represents a type that can create a writer (an object that implements `std::io::Writer`) - Add an implementation of that trait that wraps `std::io::stdout` - Add a new type parameter, `W`, to `FmtSubscriber` and it's `Builder`, that allows overriding the default `NewWriter` implementation, which is `NewStdout`
- add implementation of `NewWriter` for functions that return an object that implements `std::io::Writer` - update the default type and value for the `W` type parameter of `FmtSubscriber` and it's `Builder` to directly use `std::io::stdout`
tracing-fmt/src/lib.rs
Outdated
@@ -206,6 +215,24 @@ where | |||
} | |||
} | |||
|
|||
pub trait NewWriter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took this name from the proposal in #225, but let me know if a different name is desired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to consider renaming this to MakeWriter
, for consistency with the MakeVisitor
trait that will replace NewVisitor
in #241 --- see tower-rs/tower#108 (comment) and #240 (comment) for a discussion of this naming.
I'm having issues trying to get a mock Unfortunately, I can't relax the immutable |
- Add `MockWriter` that proxies `io::Write` implementation to a mutex-protected byte vector - Add unit test that uses `MockWriter` with a shared byte buffer to test `FmtSubscriber`'s behavior when customizing the writer
I finally figured out how to create a mock writer with a |
- move `NewWriter` and related code (including the unit test) into a `writer` submodule - move the `extern crate` statement for `lazy_static` to the top of `lib.rs` - remove now-unnecessary TODO comment in `FmtSubscriber::event`
- consolidate common logic for both `Builder::with_writer` unit tests - replace wildcard `use` statement with specific types used from the crate root
- move trait bounds for type parameter `T` in `writer::test::test_writer` to a `where` clause
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really close! I noticed some issues with the links in RustDoc that I think it would be good to fix. In addition, I commented on some code style nits, but those are not blockers.
I think this will be ready to merge once this round of feedback has been addressed! Thanks for adding this.
- rename `NewWriter` to `MakeWriter`, and its method `new_writer` to `make_writer`
- replace intra-rustdoc links with URL reference links
- add a `stderr` example for `Builder::make_writer`
- move the impl block for `Builder::make_writer` back to the create root with the other impl blocks
- update the `Builder::make_writer` unit tests to not be dependent on ANSI codes - use Edition 2018 idioms for importing the `lazy_static` macro
tracing-fmt/src/writer.rs
Outdated
/// return an instance of [`io::Write`], such as [`io::stdout`] and [`io::stderr`]. | ||
/// | ||
/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html | ||
/// [`FmtSubscriber`]: struct.FmtSubscriber.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This link will work from the trait.MakeWriter.html
page at the create root (e.g. https://docs.rs/tracing-fmt/latest/tracing_fmt/trait.MakeWriter.html) but not from the writer
sub-module page (e.g. https://docs.rs/tracing-fmt/latest/tracing_fmt/writer/trait.MakeWriter.html). Would you like me to use an absolute URL instead?
I also noticed a similar issue with tracing::Subscriber
; compare the links to Interest
on these two pages:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yeah, we should probably fix that.
My concern with absolute URLs is that if we use an absolute docs.rs URL, then we'll link to docs.rs even in local builds or (eventually) master RustDoc built on CI, which could be potentially confusing. It's possible that there's no solution that avoids this while also fixing the problem you mentioned though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, updated links to FmtSubscriber
to use absolute https://docs.rs URLs. I had to use a specific version instead of latest
since latest
points to the empty holding version 0.1.0
.
- use absolute URLs for links to `FmtSubscriber`
Great, this looks good to me! Thanks for all your work on this @jarrodldavis! |
Motivation
Closes #225
Solution
NewWriter
, to abstract over the action of creating a new object that implementsstd::io::Write
FmtSubscriber
and itsBuilder
for that traitstd::io::stdout