tomaka / rouille

Web framework in Rust
Apache License 2.0
1.09k stars 105 forks source link

add log function that uses the `log` crate #158

Closed jaemk closed 6 years ago

jaemk commented 6 years ago

It'd be nice to have the option to incorporate request logging with an app's env_logger or whatever logging backend is being used.

The name is up for debate, not sure what else to call an alternate logging function.

tomaka commented 6 years ago

I'm not familiar enough with the log crate. Will we need to bump the major version of rouille if the log crate gets bumped to 0.4 in the future?

jaemk commented 6 years ago

Ah, that's a good point. The "loggers" implement traits from the log crate, so yeah it seems you would need to keep the log crate up to date to work with the latest "loggers". That's unfortunate. Maybe the alternate logging function could instead return a tuple containing the formatted log message? Then it can be passed onto whatever logger is being used.

// still not sure about the name
fn request_info<F: FnOnce()> -> Response>(rq: &Request, f: F) -> (String, Response);
jaemk commented 6 years ago

Scratch that. Returning a message wouldn't work when the handler panics

A better solution would probably be to accept logging functions:

fn log_custom<F, L, E>(rq: &Request, log_ok_f: L, log_err_f: E, handler: F) -> Response
    where F: FnOnce() -> Response,
          L: Fn(&Request, &Response, std::time::Duration),
          E: Fn(&Request, std::time::Duration)
jaemk commented 6 years ago

Alright, I've updated to the log_custom signature above. Also updated rouille::log docs and bumped chrono to 0.4.0 (only used internally by rouille::log).