Created
February 2, 2023 14:34
-
-
Save allquixotic/a7ba9c3aa4f9ebd747c32e145a54ca2b to your computer and use it in GitHub Desktop.
Rust issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::io::Write; | |
use std::sync::Mutex; | |
use dotenvy::var; | |
se std::sync::Arc; | |
use tracing::Level; | |
struct MultiWriter { | |
writers: Vec<Arc<dyn Write>>, | |
} | |
impl Write for MultiWriter { | |
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | |
for writer in self.writers.iter_mut() { | |
writer.write(buf)?; | |
} | |
Ok(buf.len()) | |
} | |
fn flush(&mut self) -> std::io::Result<()> { | |
for writer in self.writers.iter_mut() { | |
writer.flush()?; | |
} | |
Ok(()) | |
} | |
} | |
#[tokio::main] | |
async fn main() -> anyhow::Result<()> { | |
let mut writers: Vec<Arc<dyn Write>> = vec![(Arc::new(std::io::stderr()))]; | |
if let Some(log_file) = var("log_file").ok() { | |
writers.push(Arc::new(File::create(log_file).unwrap())); | |
} | |
let mw = Mutex::new(MultiWriter { writers }); | |
let tsb = tracing_subscriber::FmtSubscriber::builder() | |
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()).with_ansi(false) | |
.with_writer(mw); | |
if let Ok(log_level) = var("log_level") { | |
match log_level.to_uppercase().as_str() { | |
"TRACE" => tsb.with_max_level(Level::TRACE), | |
"DEBUG" => tsb.with_max_level(Level::DEBUG), | |
"INFO" => tsb.with_max_level(Level::INFO), | |
"WARN" => tsb.with_max_level(Level::WARN), | |
"ERROR" => tsb.with_max_level(Level::ERROR), | |
_ => tsb.with_max_level(Level::INFO) | |
} | |
.try_init().expect("setting default subscriber failed"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0599]: the method `try_init` exists for struct `SubscriberBuilder<DefaultFields, Format, tracing::level_filters::LevelFilter, std::sync::Mutex<MultiWriter>>`, but its trait bounds were not satisfied | |
--> src/main.rs:131:10 | |
| | |
131 | .try_init().expect("setting default subscriber failed"); | |
| ^^^^^^^^ method cannot be called on `SubscriberBuilder<DefaultFields, Format, tracing::level_filters::LevelFilter, std::sync::Mutex<MultiWriter>>` due to unsatisfied trait bounds | |
| | |
::: /Users/sean/.cargo/registry/src/github.com-1ecc6299db9ec823/tracing-subscriber-0.3.16/src/fmt/fmt_layer.rs:62:1 | |
| | |
62 | / pub struct Layer< | |
63 | | S, | |
64 | | N = format::DefaultFields, | |
65 | | E = format::Format<format::Full>, | |
66 | | W = fn() -> io::Stdout, | |
67 | | > { | |
| | - | |
| | | | |
| |_doesn't satisfy `_: std::marker::Send` | |
| doesn't satisfy `_: std::marker::Sync` | |
| | |
= note: the following trait bounds were not satisfied: | |
`tracing_subscriber::fmt::Layer<Registry, DefaultFields, Format, std::sync::Mutex<MultiWriter>>: std::marker::Send` | |
`tracing_subscriber::fmt::Layer<Registry, DefaultFields, Format, std::sync::Mutex<MultiWriter>>: std::marker::Sync` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment