Skip to content
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

add tide-log #222

Merged
merged 4 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"tide",
"tide-log",
"tide-compression",
"tide-cookies",
"tide-core",
Expand Down
24 changes: 24 additions & 0 deletions tide-log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = [
"Tide Developers"
]
description = "Tide middleware for logging"
documentation = "https://docs.rs/tide-log"
keywords = ["tide", "web", "async", "middleware", "logging"]
categories = [
"logging",
"network-programming",
"web-programming::http-server",
]
edition = "2018"
license = "MIT OR Apache-2.0"
name = "tide-log"
readme = "README.md"
repository = "https://github.com/rustasync/tide"
version = "0.1.0"

[dependencies]
tide = { path = "../tide" }
futures-preview = "0.3.0-alpha.16"
http = "0.1"
log = "0.4.6"
65 changes: 65 additions & 0 deletions tide-log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#![feature(async_await)]
#![deny(
nonstandard_style,
rust_2018_idioms,
future_incompatible,
missing_debug_implementations
)]
Nemo157 marked this conversation as resolved.
Show resolved Hide resolved

use futures::future::BoxFuture;
use log::{info, trace};
use tide::{
middleware::{Middleware, Next},
Context, Response,
};

macro_rules! box_async {
fairingrey marked this conversation as resolved.
Show resolved Hide resolved
{$($t:tt)*} => {
::futures::future::FutureExt::boxed(async move { $($t)* })
};
}

/// A simple requests logger
///
/// # Examples
///
/// ```rust
///
/// let mut app = tide::App::new();
/// app.middleware(tide_log::RequestLogger::new());
/// ```
#[derive(Debug, Clone, Default)]
pub struct RequestLogger;

impl RequestLogger {
pub fn new() -> Self {
Self::default()
}

async fn log_basic<'a, Data: Send + Sync + 'static>(
&'a self,
ctx: Context<Data>,
next: Next<'a, Data>,
) -> tide::Response {
let path = ctx.uri().path().to_owned();
let method = ctx.method().as_str().to_owned();
trace!("IN => {} {}", method, path);
let start = std::time::Instant::now();
let res = next.run(ctx).await;
let status = res.status();
info!(
"{} {} {} {}ms",
method,
path,
status.as_str(),
start.elapsed().as_millis()
);
res
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for RequestLogger {
fn handle<'a>(&'a self, ctx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
box_async! { self.log_basic(ctx, next).await }
fairingrey marked this conversation as resolved.
Show resolved Hide resolved
}
}