Skip to content

Using anstream with tracing_appender's RollingFileAppender #220

@Chaoses-Ib

Description

@Chaoses-Ib

anstream's StripStream requires S: AsLockedWrite to implement std::io::Write. AsLockedWrite is implemented for Box<dyn std::io::Write> and tracing_appender's RollingFileAppender implements std::io::Write, so it looks like what is needed is just a Box. However, it doesn't actually work:

let file_appender = tracing_appender::rolling::daily(logs, "prefix");
let stream = Box::new(file_appender) as Box<dyn std::io::Write + Send>;
let strip_stream = anstream::StripStream::new(stream);
let (non_blocking, guard) = tracing_appender::non_blocking(strip_stream);
error[E0277]: the trait bound `Box<dyn std::io::Write + Send>: AsLockedWrite` is not satisfied
   --> src\main.rs:26:72
    |
26  |             let (non_blocking, guard) = tracing_appender::non_blocking(strip_stream);
    |                                         ------------------------------ ^^^^^^^^^^^^ the trait `AsLockedWrite` is not implemented for `Box<dyn std::io::Write + Send>`, which is required by `StripStream<Box<dyn std::io::Write + Send>>: std::io::Write`
    |                                         |
    |                                         required by a bound introduced by this call
    |
    = help: the trait `AsLockedWrite` is implemented for `Box<dyn std::io::Write>`
    = note: required for `StripStream<Box<dyn std::io::Write + Send>>` to implement `std::io::Write`
note: required by a bound in `non_blocking`
   --> C:\Users\Chaoses\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tracing-appender-0.2.3\src\lib.rs:193:24
    |
193 | pub fn non_blocking<T: Write + Send + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
    |                        ^^^^^ required by this bound in `non_blocking`

For more information about this error, try `rustc --explain E0277`.

This error message doesn't make sense since stream already satisfies Box<dyn std::io::Write + Send>. And if I remove Send, there will be another error:

error[E0277]: `dyn std::io::Write` cannot be sent between threads safely
   --> src\main.rs:26:72
    |
26  |             let (non_blocking, guard) = tracing_appender::non_blocking(strip_stream);
    |                                         ------------------------------ ^^^^^^^^^^^^ `dyn std::io::Write` cannot be sent between threads safely
    |                                         |
    |                                         required by a bound introduced by this call
    |
    = help: the trait `Send` is not implemented for `dyn std::io::Write`, which is required by `StripStream<Box<dyn std::io::Write>>: Send`
    = note: required for `Unique<dyn std::io::Write>` to implement `Send`
note: required because it appears within the type `Box<dyn std::io::Write>`
   --> C:\Users\Chaoses\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\boxed.rs:234:12
    |
234 | pub struct Box<
    |            ^^^
note: required because it appears within the type `StripStream<Box<dyn std::io::Write>>`
   --> C:\Users\Chaoses\.cargo\registry\src\index.crates.io-6f17d22bba15001f\anstream-0.6.15\src\strip.rs:7:12
    |
7   | pub struct StripStream<S>
    |            ^^^^^^^^^^^
note: required by a bound in `non_blocking`
   --> C:\Users\Chaoses\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tracing-appender-0.2.3\src\lib.rs:193:32
    |
193 | pub fn non_blocking<T: Write + Send + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
    |                                ^^^^ required by this bound in `non_blocking`

For more information about this error, try `rustc --explain E0277`.

Any idea about what's going wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-styleArea: anstyleC-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions