Last active
February 19, 2016 19:34
-
-
Save thetyrelcorporation/a235b30dd6b6a27e8464 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extern crate iron; | |
use iron::prelude::*; | |
use iron::{BeforeMiddleware, typemap}; | |
use std::io::prelude::*; | |
use std::fs::File; | |
use std::fs::OpenOptions; | |
pub struct RequestLog; | |
impl typemap::Key for RequestLog { type Value = u64; } | |
impl BeforeMiddleware for RequestLog { | |
fn before(&self, req: &mut Request) -> IronResult<()> { | |
let mut log_entry = "\n\n-----------------------------------------------------------------------\n".to_string(); | |
log_entry = log_entry + &format!("REQUEST\n"); | |
log_entry = log_entry + &format!("-----------------------------------------------------------------------\n"); | |
log_entry = log_entry + &format!("{:?}\n", req); | |
log_entry = log_entry + &format!("-----------------------------------------------------------------------\n\n"); | |
println!("{}", log_entry); | |
let mut file_request_result = OpenOptions::new().create(true).write(true).append(true).open("log/requests.log"); | |
match file_request_result { | |
Ok(mut log_file) => { | |
log_file.write_all(log_entry.as_bytes()); | |
}, | |
Err(err) => { | |
println!("Error obtaining log file io: {}", err); | |
}, | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment