diff --git a/Cargo.toml b/Cargo.toml index e86c204..68ed0c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,15 +16,16 @@ actix-rt = "1.1.1" actix-web = "3.0.0-alpha.2" bytes = "0.5.0" futures = "0.3.4" -log = "0.4.8" mime = "0.3.16" thiserror = "1.0" tokio = { version = "0.2.21", features = ["sync"] } +tracing = "0.1.15" +tracing-futures = "0.2.4" [dev-dependencies] actix-fs = { git = "https://git.asonix.dog/asonix/actix-fs" } anyhow = "1.0" -env_logger = "0.7.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "1.0" +tracing-subscriber = { version = "0.2.5", features = ["fmt", "tracing-log"] } diff --git a/examples/upload.rs b/examples/upload.rs index 91fac8b..691c621 100644 --- a/examples/upload.rs +++ b/examples/upload.rs @@ -7,7 +7,6 @@ use actix_web::{ }; use bytes::Bytes; use futures::stream::{Stream, TryStreamExt}; -use log::info; use std::{ env, pin::Pin, @@ -16,6 +15,7 @@ use std::{ Arc, }, }; +use tracing::info; #[derive(Clone, Debug)] struct AppState { @@ -85,8 +85,12 @@ async fn save_file( #[actix_rt::main] async fn main() -> Result<(), anyhow::Error> { - env::set_var("RUST_LOG", "upload=info"); - env_logger::init(); + if env::var("RUST_LOG").is_err() { + env::set_var("RUST_LOG", "upload=info"); + } + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .init(); let file_count = Arc::new(AtomicUsize::new(0)); diff --git a/src/spawn.rs b/src/spawn.rs index 898eda5..fb59db6 100644 --- a/src/spawn.rs +++ b/src/spawn.rs @@ -1,4 +1,4 @@ -use log::error; +use tracing::error; use std::future::Future; use tokio::sync::oneshot::channel; diff --git a/src/types.rs b/src/types.rs index b62a13b..5a1235f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -20,7 +20,7 @@ use crate::Error; use bytes::Bytes; use futures::Stream; -use log::trace; +use tracing::trace; use mime::Mime; use std::{ collections::{HashMap, VecDeque}, diff --git a/src/upload.rs b/src/upload.rs index b4fca7d..b9b79dc 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -29,7 +29,6 @@ use futures::{ select, stream::{FuturesUnordered, StreamExt}, }; -use log::trace; use std::{ collections::HashMap, path::Path, @@ -38,6 +37,8 @@ use std::{ Arc, }, }; +use tracing::{trace, Span}; +use tracing_futures::Instrument; fn consolidate(mf: MultipartForm) -> Value { mf.into_iter().fold( @@ -211,6 +212,8 @@ async fn handle_stream_field( /// Handle multipart streams from Actix Web pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Result { + let parent = Span::current(); + let mut multipart_form = Vec::new(); let mut file_count: u32 = 0; let mut field_count: u32 = 0; @@ -223,9 +226,7 @@ pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Resu select! { opt = m.next() => { if let Some(res) = opt { - let field = res?; - - unordered.push(crate::spawn(handle_stream_field(field, form.clone()))); + unordered.push(process_field(res?, form.clone(), &parent)); } } opt = unordered.next() => { @@ -246,6 +247,16 @@ pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Resu Ok(consolidate(multipart_form)) } +async fn process_field( + field: actix_multipart::Field, + form: Form, + parent: &Span, +) -> Result, crate::spawn::Canceled> { + let span = tracing::info_span!(parent: parent, "field"); + + crate::spawn(handle_stream_field(field, form).instrument(span)).await +} + fn count( content: &MultipartContent, mut file_count: u32,