-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff5ca1
commit 019d13f
Showing
3 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use log; | ||
use std_logger::REQUEST_TARGET; | ||
|
||
fn main() { | ||
// Initialize the logger. | ||
std_logger::init(); | ||
|
||
// Fake the handling of a request. | ||
logger_middleware(Request { | ||
url: "/".to_owned(), | ||
method: "GET".to_owned(), | ||
}); | ||
} | ||
|
||
// Our fake HTTP request. | ||
struct Request { | ||
url: String, | ||
method: String, | ||
} | ||
|
||
// Our fake HTTP response. | ||
struct Response { | ||
status_code: u16, | ||
body: String, | ||
} | ||
|
||
fn logger_middleware(request: Request) -> Response { | ||
// Clone the url and method. Note: don't actually do this in an HTTP this is | ||
// rather wastefull to. | ||
let url = request.url.clone(); | ||
let method = request.url.clone(); | ||
|
||
// Call our handler. | ||
let response = http_handler(request); | ||
|
||
log::info!("Hello world"); | ||
|
||
let kvs: Vec<Box<dyn log::kv::Source>> = vec![ | ||
Box::new(("url", &url)), | ||
Box::new(("method", &method)), | ||
Box::new(("status_code", response.status_code)), | ||
Box::new(("body_size", response.body.len() as u64)), | ||
]; | ||
|
||
let record = log::Record::builder() | ||
.args(format_args!("got request")) | ||
.level(log::Level::Info) | ||
.target(REQUEST_TARGET) | ||
.file(Some(file!())) | ||
.line(Some(line!())) | ||
.module_path(Some(module_path!())) | ||
.key_values(&kvs) | ||
.build(); | ||
log::logger().log(&record); | ||
|
||
let record = log::Record::builder() | ||
.args(format_args!("some message")) | ||
.level(log::Level::Info) | ||
.target("some_target") | ||
.file(Some(file!())) | ||
.line(Some(line!())) | ||
.module_path(Some(module_path!())) | ||
.key_values(&("single", "value")) | ||
.build(); | ||
log::logger().log(&record); | ||
|
||
response | ||
} | ||
|
||
fn http_handler(request: Request) -> Response { | ||
match (request.method.as_str(), request.url.as_str()) { | ||
("GET", "/") => Response { | ||
status_code: 200, | ||
body: "Home page".to_owned(), | ||
}, | ||
_ => Response { | ||
status_code: 404, | ||
body: "Not found".to_owned(), | ||
}, | ||
} | ||
} |
This file contains 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