From 8b06916e75de05b8ba34021f07b3e897479857e3 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 21 May 2019 16:50:14 +0530 Subject: [PATCH 1/7] isolate tide-headers, tide-slog; better; improve loggers --- Cargo.toml | 11 ++-- src/{middleware/mod.rs => middleware.rs} | 10 ++-- tide-headers/Cargo.toml | 23 ++++++++ .../src/lib.rs | 19 +++++-- tide-log/Cargo.toml | 6 +-- tide-log/src/lib.rs | 29 ++++++++-- tide-slog/Cargo.toml | 27 ++++++++++ .../logger.rs => tide-slog/src/lib.rs | 54 +++++++++++++------ 8 files changed, 142 insertions(+), 37 deletions(-) rename src/{middleware/mod.rs => middleware.rs} (53%) create mode 100644 tide-headers/Cargo.toml rename src/middleware/default_headers.rs => tide-headers/src/lib.rs (79%) create mode 100644 tide-slog/Cargo.toml rename src/middleware/logger.rs => tide-slog/src/lib.rs (51%) diff --git a/Cargo.toml b/Cargo.toml index a8fdb55e7..61250f838 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,11 +30,10 @@ http-service = "0.2.0" serde = "1.0.91" serde_derive = "1.0.91" serde_urlencoded = "0.5.5" -slog = "2.4.1" -slog-async = "2.3.0" -slog-term = "2.4.0" tide-cookies = { path = "./tide-cookies", optional = true } tide-core = { path = "./tide-core" } +tide-headers = { path = "./tide-headers" } +tide-log = { path = "./tide-log" } [dependencies.multipart] default-features = false @@ -52,13 +51,19 @@ mime = "0.3.13" mime_guess = "2.0.0-alpha.6" percent-encoding = "1.0.1" serde = { version = "1.0.91", features = ["derive"] } +tide-log = { path = "./tide-log" } +env_logger = "0.6.1" +log4rs = "0.8.3" +log = "0.4.6" [workspace] members = [ "tide-compression", "tide-cookies", "tide-core", + "tide-headers", "tide-log", + "tide-slog", ] [patch.crates-io] diff --git a/src/middleware/mod.rs b/src/middleware.rs similarity index 53% rename from src/middleware/mod.rs rename to src/middleware.rs index aaa20d48b..581792a7a 100644 --- a/src/middleware/mod.rs +++ b/src/middleware.rs @@ -1,7 +1,9 @@ -mod default_headers; -mod logger; +// Core +pub use tide_core::middleware::{Middleware, Next}; + +// Exports from tide repo. +pub use tide_headers::DefaultHeaders; +pub use tide_log::RequestLogger; -pub use self::{default_headers::DefaultHeaders, logger::RootLogger}; #[cfg(feature = "cookies")] pub use tide_cookies::CookiesMiddleware; -pub use tide_core::middleware::{Middleware, Next}; diff --git a/tide-headers/Cargo.toml b/tide-headers/Cargo.toml new file mode 100644 index 000000000..0d02e69a0 --- /dev/null +++ b/tide-headers/Cargo.toml @@ -0,0 +1,23 @@ +[package] +authors = [ + "Tide Developers" +] +description = "Header related middleware for tide" +documentation = "https://docs.rs/tide-log" +keywords = ["tide", "web", "async", "middleware", "headers"] +categories = [ + "network-programming", + "web-programming::http-server", +] +edition = "2018" +license = "MIT OR Apache-2.0" +name = "tide-headers" +readme = "README.md" +repository = "https://github.com/rustasync/tide" +version = "0.1.0" + + [dependencies] +tide-core = { path = "../tide-core" } +futures-preview = "0.3.0-alpha.16" +http = "0.1" +log = "0.4.6" diff --git a/src/middleware/default_headers.rs b/tide-headers/src/lib.rs similarity index 79% rename from src/middleware/default_headers.rs rename to tide-headers/src/lib.rs index d76dbb192..36b080567 100644 --- a/src/middleware/default_headers.rs +++ b/tide-headers/src/lib.rs @@ -1,12 +1,23 @@ +//! Crate that provides helpers, and/or middlewares for tide +//! related to http headers. +#![feature(async_await)] +#![warn( + nonstandard_style, + rust_2018_idioms, + future_incompatible, + missing_debug_implementations +)] + use futures::future::BoxFuture; use futures::prelude::*; +use log::trace; use http::{ header::{HeaderValue, IntoHeaderName}, HeaderMap, HttpTryFrom, }; -use crate::{ +use tide_core::{ middleware::{Middleware, Next}, Context, Response, }; @@ -20,10 +31,9 @@ pub struct DefaultHeaders { impl DefaultHeaders { /// Construct a new instance with an empty list of headers. pub fn new() -> DefaultHeaders { - DefaultHeaders::default() + Self::default() } - #[inline] /// Add a header to the default header list. pub fn header(mut self, key: K, value: V) -> Self where @@ -35,7 +45,6 @@ impl DefaultHeaders { .expect("Cannot create default header"); self.headers.append(key, value); - self } } @@ -44,9 +53,9 @@ impl Middleware for DefaultHeaders { fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let mut res = next.run(cx).await; - let headers = res.headers_mut(); for (key, value) in self.headers.iter() { + trace!("add default: {} {:?}", &key, &value); headers.entry(key).unwrap().or_insert_with(|| value.clone()); } res diff --git a/tide-log/Cargo.toml b/tide-log/Cargo.toml index 515dda102..24671ec59 100644 --- a/tide-log/Cargo.toml +++ b/tide-log/Cargo.toml @@ -2,7 +2,7 @@ authors = [ "Tide Developers" ] -description = "Tide middleware for logging" +description = "Logging middleware for tide" documentation = "https://docs.rs/tide-log" keywords = ["tide", "web", "async", "middleware", "logging"] categories = [ @@ -18,7 +18,7 @@ repository = "https://github.com/rustasync/tide" version = "0.1.0" [dependencies] -tide = { path = "../" } +tide-core = { path = "../tide-core" } futures-preview = "0.3.0-alpha.16" http = "0.1" -log = "0.4.6" +log = "0.4.6" diff --git a/tide-log/src/lib.rs b/tide-log/src/lib.rs index 91f057031..a7a875014 100644 --- a/tide-log/src/lib.rs +++ b/tide-log/src/lib.rs @@ -1,3 +1,6 @@ +//! Crate that provides helpers and/or middlewares for tide +//! related to logging. +//! #![feature(async_await)] #![warn( nonstandard_style, @@ -9,7 +12,8 @@ use futures::future::BoxFuture; use futures::prelude::*; use log::{info, trace}; -use tide::{ + +use tide_core::{ middleware::{Middleware, Next}, Context, Response, }; @@ -24,25 +28,40 @@ use tide::{ /// app.middleware(tide_log::RequestLogger::new()); /// ``` #[derive(Debug, Clone, Default)] -pub struct RequestLogger; +pub struct RequestLogger { + target: String, +} impl RequestLogger { + /// Create a new instance of logger with default target as + /// "requests" pub fn new() -> Self { - Self::default() + Self { + target: "requests".to_owned(), + } + } + + /// Create a new instance of logger with supplied `target` for + /// logging. + pub fn with_target(target: String) -> Self { + Self { + target, + } } async fn log_basic<'a, Data: Send + Sync + 'static>( &'a self, ctx: Context, next: Next<'a, Data>, - ) -> tide::Response { + ) -> Response { let path = ctx.uri().path().to_owned(); let method = ctx.method().as_str().to_owned(); - trace!("IN => {} {}", method, path); + trace!(target: &self.target, "IN => {} {}", method, path); let start = std::time::Instant::now(); let res = next.run(ctx).await; let status = res.status(); info!( + target: &self.target, "{} {} {} {}ms", method, path, diff --git a/tide-slog/Cargo.toml b/tide-slog/Cargo.toml new file mode 100644 index 000000000..92487dae0 --- /dev/null +++ b/tide-slog/Cargo.toml @@ -0,0 +1,27 @@ +[package] +authors = [ + "Tide Developers" +] +description = "Logging middleware for tide based on slog" +documentation = "https://docs.rs/tide-log" +keywords = ["tide", "web", "async", "middleware", "logging", "slog"] +categories = [ + "logging", + "network-programming", + "web-programming::http-server", +] +edition = "2018" +license = "MIT OR Apache-2.0" +name = "tide-slog" +readme = "README.md" +repository = "https://github.com/rustasync/tide" +version = "0.1.0" + + [dependencies] +tide-core = { path = "../tide-core" } +futures-preview = "0.3.0-alpha.16" +http = "0.1" +log = "0.4.6" +slog = "2.4.1" +slog-async = "2.3.0" +slog-term = "2.4.0" diff --git a/src/middleware/logger.rs b/tide-slog/src/lib.rs similarity index 51% rename from src/middleware/logger.rs rename to tide-slog/src/lib.rs index 8036cf585..121f45e8c 100644 --- a/src/middleware/logger.rs +++ b/tide-slog/src/lib.rs @@ -1,50 +1,70 @@ -use slog::{info, o, Drain}; +#![feature(async_await)] +#![warn( + nonstandard_style, + rust_2018_idioms, + future_incompatible, + missing_debug_implementations +)] + +use slog::{info, trace, o, Drain}; use slog_async; use slog_term; use futures::future::BoxFuture; use futures::prelude::*; -use crate::{ +use tide_core::{ middleware::{Middleware, Next}, Context, Response, }; -/// Root logger for Tide. Wraps over logger provided by slog.SimpleLogger +/// RequestLogger based on slog.SimpleLogger #[derive(Debug)] -pub struct RootLogger { +pub struct RequestLogger { // drain: dyn slog::Drain, - inner_logger: slog::Logger, + inner: slog::Logger, } -impl RootLogger { - pub fn new() -> RootLogger { - let decorator = slog_term::TermDecorator::new().build(); - let drain = slog_term::CompactFormat::new(decorator).build().fuse(); - let drain = slog_async::Async::new(drain).build().fuse(); +impl RequestLogger { + pub fn new() -> Self { + Default::default() + } - let log = slog::Logger::root(drain, o!()); - RootLogger { inner_logger: log } + pub fn with_logger(logger: slog::Logger) -> Self { + Self { inner: logger } } } -impl Default for RootLogger { +impl Default for RequestLogger { fn default() -> Self { - Self::new() + let decorator = slog_term::TermDecorator::new().build(); + let drain = slog_term::CompactFormat::new(decorator).build().fuse(); + let drain = slog_async::Async::new(drain).build().fuse(); + + let log = slog::Logger::root(drain, o!()); + Self { inner: log } } } /// Stores information during request phase and logs information once the response /// is generated. -impl Middleware for RootLogger { +impl Middleware for RequestLogger { fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let path = cx.uri().path().to_owned(); let method = cx.method().as_str().to_owned(); - + trace!(self.inner, "IN => {} {}", method, path); + let start = std::time::Instant::now(); let res = next.run(cx).await; let status = res.status(); - info!(self.inner_logger, "{} {} {}", method, path, status.as_str()); + info!( + self.inner, + "{} {} {} {}ms", + method, + path, + status.as_str(), + start.elapsed().as_millis() + ); res }) } From 225a43d0029a04700442e40833475d2cf7184546 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 21 May 2019 16:52:50 +0530 Subject: [PATCH 2/7] add logging examples --- examples/hello_envlog.rs | 8 ++++++++ examples/hello_logrs.rs | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 examples/hello_envlog.rs create mode 100644 examples/hello_logrs.rs diff --git a/examples/hello_envlog.rs b/examples/hello_envlog.rs new file mode 100644 index 000000000..d57a5a4bd --- /dev/null +++ b/examples/hello_envlog.rs @@ -0,0 +1,8 @@ +#![feature(async_await)] +fn main() { + env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init(); + let mut app = tide::App::new(); + app.middleware(tide::middleware::RequestLogger::new()); + app.at("/").get(async move |_| "Hello, world!"); + app.run("127.0.0.1:8000").unwrap(); +} diff --git a/examples/hello_logrs.rs b/examples/hello_logrs.rs new file mode 100644 index 000000000..d32f2696e --- /dev/null +++ b/examples/hello_logrs.rs @@ -0,0 +1,18 @@ +#![feature(async_await)] +fn main() { + use log::LevelFilter; + use log4rs::append::console::ConsoleAppender; + use log4rs::config::{Appender, Config, Root}; + + let stdout = ConsoleAppender::builder().build(); + let config = Config::builder() + .appender(Appender::builder().build("stdout", Box::new(stdout))) + .build(Root::builder().appender("stdout").build(LevelFilter::Info)) + .unwrap(); + let _handle = log4rs::init_config(config).unwrap(); + + let mut app = tide::App::new(); + app.middleware(tide::middleware::RequestLogger::new()); + app.at("/").get(async move |_| "Hello, world!"); + app.run("127.0.0.1:8000").unwrap(); +} From 7d9e136b8183156c5dd0d96ff4eff78bd537de50 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 21 May 2019 17:42:35 +0530 Subject: [PATCH 3/7] cargo fmt --- tide-log/src/lib.rs | 4 +--- tide-slog/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tide-log/src/lib.rs b/tide-log/src/lib.rs index a7a875014..88edf072b 100644 --- a/tide-log/src/lib.rs +++ b/tide-log/src/lib.rs @@ -44,9 +44,7 @@ impl RequestLogger { /// Create a new instance of logger with supplied `target` for /// logging. pub fn with_target(target: String) -> Self { - Self { - target, - } + Self { target } } async fn log_basic<'a, Data: Send + Sync + 'static>( diff --git a/tide-slog/src/lib.rs b/tide-slog/src/lib.rs index 121f45e8c..1b1724fa7 100644 --- a/tide-slog/src/lib.rs +++ b/tide-slog/src/lib.rs @@ -6,7 +6,7 @@ missing_debug_implementations )] -use slog::{info, trace, o, Drain}; +use slog::{info, o, trace, Drain}; use slog_async; use slog_term; From 8aac219292e2688779de1d390fde6e447c70f3c6 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 21 May 2019 17:59:42 +0530 Subject: [PATCH 4/7] add tide dev-dep to all crates --- tide-compression/Cargo.toml | 1 + tide-cookies/Cargo.toml | 1 + tide-headers/Cargo.toml | 3 +++ tide-log/Cargo.toml | 3 +++ tide-slog/Cargo.toml | 3 +++ 5 files changed, 11 insertions(+) diff --git a/tide-compression/Cargo.toml b/tide-compression/Cargo.toml index f00c417d5..395fff270 100644 --- a/tide-compression/Cargo.toml +++ b/tide-compression/Cargo.toml @@ -27,4 +27,5 @@ features = ["stream", "gzip", "zlib", "brotli", "zstd"] version = "0.1.0-alpha.1" [dev-dependencies] +tide = { path = "../" } http-service-mock = "0.2.0" diff --git a/tide-cookies/Cargo.toml b/tide-cookies/Cargo.toml index 34b388977..e37532055 100644 --- a/tide-cookies/Cargo.toml +++ b/tide-cookies/Cargo.toml @@ -18,4 +18,5 @@ http-service = "0.2.0" tide-core = { path = "../tide-core" } [dev-dependencies] +tide = { path = "../" } http-service-mock = "0.2.0" diff --git a/tide-headers/Cargo.toml b/tide-headers/Cargo.toml index 0d02e69a0..1340c08bd 100644 --- a/tide-headers/Cargo.toml +++ b/tide-headers/Cargo.toml @@ -21,3 +21,6 @@ tide-core = { path = "../tide-core" } futures-preview = "0.3.0-alpha.16" http = "0.1" log = "0.4.6" + +[dev-dependencies] +tide = { path = "../" } \ No newline at end of file diff --git a/tide-log/Cargo.toml b/tide-log/Cargo.toml index 24671ec59..dee006c86 100644 --- a/tide-log/Cargo.toml +++ b/tide-log/Cargo.toml @@ -22,3 +22,6 @@ tide-core = { path = "../tide-core" } futures-preview = "0.3.0-alpha.16" http = "0.1" log = "0.4.6" + +[dev-dependencies] +tide = { path = "../" } diff --git a/tide-slog/Cargo.toml b/tide-slog/Cargo.toml index 92487dae0..ef6ddb36a 100644 --- a/tide-slog/Cargo.toml +++ b/tide-slog/Cargo.toml @@ -25,3 +25,6 @@ log = "0.4.6" slog = "2.4.1" slog-async = "2.3.0" slog-term = "2.4.0" + +[dev-dependencies] +tide = { path = "../" } \ No newline at end of file From f41754bf270c7aba03df9425e9a4cf4adbc3c367 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 21 May 2019 18:11:47 +0530 Subject: [PATCH 5/7] fixup manifest documentation links --- tide-cookies/Cargo.toml | 4 ++-- tide-headers/Cargo.toml | 2 +- tide-slog/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tide-cookies/Cargo.toml b/tide-cookies/Cargo.toml index e37532055..012f61f72 100644 --- a/tide-cookies/Cargo.toml +++ b/tide-cookies/Cargo.toml @@ -5,8 +5,8 @@ edition = "2018" authors = [ "Tide Developers", ] -description = "Cookie management for Tide web framework" -documentation = "https://docs.rs/tide-core" +description = "Cookie middleware and extensions for tide" +documentation = "https://docs.rs/tide-cookies" license = "MIT OR Apache-2.0" repository = "https://github.com/rustasync/tide" diff --git a/tide-headers/Cargo.toml b/tide-headers/Cargo.toml index 1340c08bd..04ff51499 100644 --- a/tide-headers/Cargo.toml +++ b/tide-headers/Cargo.toml @@ -3,7 +3,7 @@ authors = [ "Tide Developers" ] description = "Header related middleware for tide" -documentation = "https://docs.rs/tide-log" +documentation = "https://docs.rs/tide-headers" keywords = ["tide", "web", "async", "middleware", "headers"] categories = [ "network-programming", diff --git a/tide-slog/Cargo.toml b/tide-slog/Cargo.toml index ef6ddb36a..dfddda8f3 100644 --- a/tide-slog/Cargo.toml +++ b/tide-slog/Cargo.toml @@ -3,7 +3,7 @@ authors = [ "Tide Developers" ] description = "Logging middleware for tide based on slog" -documentation = "https://docs.rs/tide-log" +documentation = "https://docs.rs/tide-slog" keywords = ["tide", "web", "async", "middleware", "logging", "slog"] categories = [ "logging", From 476d75b57ffc10ae8ca9c30af15488cd64832870 Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 21 May 2019 07:43:30 -0700 Subject: [PATCH 6/7] Update Cargo.toml add newline before EOF --- tide-headers/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tide-headers/Cargo.toml b/tide-headers/Cargo.toml index 04ff51499..f53dff0ed 100644 --- a/tide-headers/Cargo.toml +++ b/tide-headers/Cargo.toml @@ -23,4 +23,4 @@ http = "0.1" log = "0.4.6" [dev-dependencies] -tide = { path = "../" } \ No newline at end of file +tide = { path = "../" } From f354835c211a30035ca997b229b9325f35b1bcc2 Mon Sep 17 00:00:00 2001 From: grey Date: Tue, 21 May 2019 07:51:53 -0700 Subject: [PATCH 7/7] capitalize Tide in crate desc --- tide-cookies/Cargo.toml | 2 +- tide-headers/Cargo.toml | 2 +- tide-log/Cargo.toml | 2 +- tide-slog/Cargo.toml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tide-cookies/Cargo.toml b/tide-cookies/Cargo.toml index 012f61f72..6c0742f4e 100644 --- a/tide-cookies/Cargo.toml +++ b/tide-cookies/Cargo.toml @@ -5,7 +5,7 @@ edition = "2018" authors = [ "Tide Developers", ] -description = "Cookie middleware and extensions for tide" +description = "Cookie middleware and extensions for Tide" documentation = "https://docs.rs/tide-cookies" license = "MIT OR Apache-2.0" repository = "https://github.com/rustasync/tide" diff --git a/tide-headers/Cargo.toml b/tide-headers/Cargo.toml index f53dff0ed..294bc7791 100644 --- a/tide-headers/Cargo.toml +++ b/tide-headers/Cargo.toml @@ -2,7 +2,7 @@ authors = [ "Tide Developers" ] -description = "Header related middleware for tide" +description = "Header related middleware for Tide" documentation = "https://docs.rs/tide-headers" keywords = ["tide", "web", "async", "middleware", "headers"] categories = [ diff --git a/tide-log/Cargo.toml b/tide-log/Cargo.toml index dee006c86..4a2074b85 100644 --- a/tide-log/Cargo.toml +++ b/tide-log/Cargo.toml @@ -2,7 +2,7 @@ authors = [ "Tide Developers" ] -description = "Logging middleware for tide" +description = "Logging middleware for Tide" documentation = "https://docs.rs/tide-log" keywords = ["tide", "web", "async", "middleware", "logging"] categories = [ diff --git a/tide-slog/Cargo.toml b/tide-slog/Cargo.toml index dfddda8f3..ca23079b8 100644 --- a/tide-slog/Cargo.toml +++ b/tide-slog/Cargo.toml @@ -2,7 +2,7 @@ authors = [ "Tide Developers" ] -description = "Logging middleware for tide based on slog" +description = "Logging middleware for Tide based on slog" documentation = "https://docs.rs/tide-slog" keywords = ["tide", "web", "async", "middleware", "logging", "slog"] categories = [ @@ -27,4 +27,4 @@ slog-async = "2.3.0" slog-term = "2.4.0" [dev-dependencies] -tide = { path = "../" } \ No newline at end of file +tide = { path = "../" }