From f1b39b1ea82ce43a5185ec1869e574b2f34ac2d4 Mon Sep 17 00:00:00 2001 From: Kilerd Chan Date: Mon, 27 May 2019 11:58:51 +0800 Subject: [PATCH] feat: add logger --- Cargo.lock | 1 + Cargo.toml | 1 + Dockerfile | 3 +++ src/main.rs | 2 ++ src/routers/admin.rs | 17 +++++++++++++++-- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fa26cd6..5a384f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1601,6 +1601,7 @@ dependencies = [ "futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.22 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 4d894b1..600c7e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,4 @@ time = "0.1.42" rss = "1.7.0" diesel_derives = "1.4.0" openssl = "0.10.22" +log = "0.4.6" diff --git a/Dockerfile b/Dockerfile index fcd85fa..5623f3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,6 +29,9 @@ COPY --from=builder /app/rubble/target/x86_64-unknown-linux-musl/release/rubble EXPOSE 8000 ENV DATABASE_URL postgres://root@postgres/rubble +ENV LOG info WORKDIR /application + +RUN export RUST_LOG=rubble=$LOG CMD ["./rubble"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0df1fa8..935bf5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ extern crate diesel; extern crate diesel_derives; #[macro_use] extern crate diesel_migrations; +#[macro_use] +extern crate log; use actix_web::{ middleware::{ diff --git a/src/routers/admin.rs b/src/routers/admin.rs index 2175c19..dbe6e5c 100644 --- a/src/routers/admin.rs +++ b/src/routers/admin.rs @@ -68,9 +68,11 @@ pub fn admin_authentication( Ok(login_user) => { if login_user.authenticated(&user.password) { id.remember(login_user.username); + info!("admin login"); RubbleResponder::Redirect("/admin/panel".into()) } else { // TODO flash message or throw unauthorized + warn!("try logining admin with wrong password '{}'", user.password); RubbleResponder::Redirect("/admin/login".into()) } } @@ -138,13 +140,21 @@ pub fn article_save( return RubbleResponder::Redirect("/admin/login".into()); } + let article_title = article.title.clone(); + let admin = User::find_by_username(&data.postgres(), &id.identity().unwrap()) .expect("cannot found this user"); let res = if let Some(article_id) = article.id { + info!("updating article #{} {}", article_id, article_title); Article::update(&data.postgres(), article_id, &article.into_inner().into()) } else { + info!("creating new article {}", article_title); Article::create(&data.postgres(), &article.into_inner().into()) }; + + if res.is_err() { + error!("error on updating/creating article {}", article_title); + } RubbleResponder::Redirect("/admin/panel".into()) } @@ -161,7 +171,9 @@ pub fn article_deletion( let admin = User::find_by_username(&data.postgres(), &id.identity().unwrap()) .expect("cannot found this user"); - Article::delete(&data.postgres(), article_id.into_inner()); + let i = article_id.into_inner(); + Article::delete(&data.postgres(), i); + info!("deleting article {}", i); RubbleResponder::Redirect("/admin/panel".into()) } @@ -180,6 +192,7 @@ pub fn change_password( admin.password = User::password_generate(&password.password).to_string(); User::update(&data.postgres(), admin.id, &admin); id.forget(); + info!("updating password"); RubbleResponder::Redirect("/admin/panel".into()) } @@ -197,7 +210,7 @@ pub fn change_setting( .expect("cannot found this user"); Setting::update(&data.postgres(), setting.name.clone(), &setting); - + info!("updating setting {:?} to {:?}", setting.name, setting.value); RubbleResponder::Redirect("/admin/panel".into()) }