diff --git a/content/docs/errors.md b/content/docs/errors.md index 50c5baf..6dd697a 100644 --- a/content/docs/errors.md +++ b/content/docs/errors.md @@ -128,6 +128,12 @@ By dividing errors into those which are user facing and those which are not, we can ensure that we don't accidentally expose users to errors thrown by application internals which they weren't meant to see. +# Error Logging + +This is a basic example using `middleware::Logger`: + +{{< include-example example="errors" file="logging.rs" section="logging" >}} + [actixerror]: https://docs.rs/actix-web/1.0.2/actix_web/error/struct.Error.html [errorhelpers]: https://docs.rs/actix-web/1.0.2/actix_web/trait.ResponseError.html [failure]: https://github.com/rust-lang-nursery/failure diff --git a/examples/errors/Cargo.toml b/examples/errors/Cargo.toml index 6fcbcbd..0b605e2 100644 --- a/examples/errors/Cargo.toml +++ b/examples/errors/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "errors" +name = "my_errors" version = "1.0.0" edition = "2018" diff --git a/examples/errors/src/logging.rs b/examples/errors/src/logging.rs new file mode 100644 index 0000000..9f5b880 --- /dev/null +++ b/examples/errors/src/logging.rs @@ -0,0 +1,38 @@ +// +use actix_web::{error, Result}; +use failure::Fail; +use log::debug; + +#[derive(Fail, Debug)] +#[fail(display = "my error")] +pub struct MyError { + name: &'static str, +} + +// Use default implementation for `error_response()` method +impl error::ResponseError for MyError {} + +pub fn index() -> Result<&'static str, MyError> { + let err = MyError { name: "test error" }; + debug!("{}", err); + Err(err) +} + +pub fn main() { + use actix_web::{middleware::Logger, web, App, HttpServer}; + + std::env::set_var("RUST_LOG", "my_errors=debug,actix_web=info"); + std::env::set_var("RUST_BACKTRACE", "1"); + env_logger::init(); + + HttpServer::new(|| { + App::new() + .wrap(Logger::default()) + .route("/", web::get().to(index)) + }) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); +} +// diff --git a/examples/errors/src/main.rs b/examples/errors/src/main.rs index f2d1762..c9e4984 100644 --- a/examples/errors/src/main.rs +++ b/examples/errors/src/main.rs @@ -1,4 +1,5 @@ pub mod helpers; +pub mod logging; pub mod override_error; pub mod recommend_one; pub mod recommend_two;