From 900a539da2994a1476c851d66e19e0375a3718ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Fri, 3 Apr 2020 14:29:30 +0200 Subject: [PATCH] Fix logger --- .env | 2 +- Cargo.lock | 12 ++++++++++++ jirs-server/Cargo.toml | 2 ++ jirs-server/src/db/authorize_user.rs | 13 ++++++++----- jirs-server/src/db/issues.rs | 14 ++++++++++---- jirs-server/src/db/mod.rs | 15 +++++---------- jirs-server/src/main.rs | 4 +++- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.env b/.env index 7d8e7170..ea5f638f 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ DATABASE_URL=postgres://postgres@localhost:5432/jirs -RUST_LOG=actix_web=info,diesel=info +RUST_LOG=debug JIRS_CLIENT_PORT=7000 JIRS_CLIENT_BIND=0.0.0.0 JIRS_SERVER_PORT=5000 diff --git a/Cargo.lock b/Cargo.lock index 2cd2857e..f97f4a02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1081,11 +1081,13 @@ dependencies = [ "ipnetwork", "jirs-data", "libc", + "log 0.4.8", "num-bigint", "num-integer", "num-traits", "percent-encoding 2.1.0", "pq-sys", + "pretty_env_logger", "quickcheck", "r2d2", "serde", @@ -1398,6 +1400,16 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "pretty_env_logger" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" +dependencies = [ + "env_logger 0.7.1", + "log 0.4.8", +] + [[package]] name = "proc-macro-hack" version = "0.5.14" diff --git a/jirs-server/Cargo.toml b/jirs-server/Cargo.toml index ad5fd275..de9cb35f 100644 --- a/jirs-server/Cargo.toml +++ b/jirs-server/Cargo.toml @@ -37,6 +37,8 @@ num-integer = { version = "0.1.32" } bigdecimal = { version = ">= 0.0.10, <= 0.1.0" } bitflags = { version = "1.0" } r2d2 = { version = ">= 0.8, < 0.9" } +log = "0.4" +pretty_env_logger = "0.4" env_logger = "0.7" futures = { version = "*" } diff --git a/jirs-server/src/db/authorize_user.rs b/jirs-server/src/db/authorize_user.rs index 72c0d816..fe09711b 100644 --- a/jirs-server/src/db/authorize_user.rs +++ b/jirs-server/src/db/authorize_user.rs @@ -1,10 +1,11 @@ -use crate::db::{DbExecutor, DbPool, SyncQuery}; -use crate::errors::ServiceErrors; -use crate::models::{Token, User}; use actix::{Handler, Message}; use diesel::prelude::*; use serde::{Deserialize, Serialize}; +use crate::db::{DbExecutor, DbPool, SyncQuery}; +use crate::errors::ServiceErrors; +use crate::models::{Token, User}; + #[derive(Serialize, Deserialize, Debug)] pub struct AuthorizeUser { pub access_token: uuid::Uuid, @@ -28,11 +29,13 @@ impl Handler for DbExecutor { let token = tokens .filter(access_token.eq(msg.access_token)) .first::(conn) - .map_err(|_e| ServiceErrors::RecordNotFound("Token".to_string()))?; + .map_err(|_e| { + ServiceErrors::RecordNotFound(format!("token for {}", msg.access_token)) + })?; let user = users .filter(id.eq(token.user_id)) .first::(conn) - .map_err(|_e| ServiceErrors::RecordNotFound("User".to_string()))?; + .map_err(|_e| ServiceErrors::RecordNotFound(format!("user {}", token.user_id)))?; Ok(user) } } diff --git a/jirs-server/src/db/issues.rs b/jirs-server/src/db/issues.rs index 15c89179..510cbade 100644 --- a/jirs-server/src/db/issues.rs +++ b/jirs-server/src/db/issues.rs @@ -57,9 +57,12 @@ impl Handler for DbExecutor { .0 .get() .map_err(|_| ServiceErrors::DatabaseConnectionLost)?; - let vec = issues - .filter(project_id.eq(msg.project_id)) - .distinct() + let chain = issues.filter(project_id.eq(msg.project_id)).distinct(); + debug!( + "{}", + diesel::debug_query::(&chain).to_string() + ); + let vec = chain .load::(conn) .map_err(|_| ServiceErrors::RecordNotFound("project issues".to_string()))?; Ok(vec) @@ -120,7 +123,10 @@ impl Handler for DbExecutor { .map(|project_id| dsl::project_id.eq(project_id)), dsl::updated_at.eq(chrono::Utc::now().naive_utc()), )); - diesel::debug_query::(&chain); + debug!( + "{}", + diesel::debug_query::(&chain).to_string() + ); chain.get_result::(conn).map_err(|_| { ServiceErrors::DatabaseQueryFailed("Failed to update issue".to_string()) })?; diff --git a/jirs-server/src/db/mod.rs b/jirs-server/src/db/mod.rs index ffc48fb9..b4ee2991 100644 --- a/jirs-server/src/db/mod.rs +++ b/jirs-server/src/db/mod.rs @@ -53,6 +53,7 @@ pub mod dev { use std::ops::Deref; use diesel::connection::{AnsiTransactionManager, SimpleConnection}; + use diesel::debug_query; use diesel::deserialize::QueryableByName; use diesel::query_builder::{AsQuery, QueryFragment, QueryId}; use diesel::sql_types::HasSqlType; @@ -72,8 +73,6 @@ pub mod dev { impl SimpleConnection for VerboseConnection { fn batch_execute(&self, query: &str) -> QueryResult<()> { - use diesel::debug_query; - debug_query::(&query); self.inner.batch_execute(query) } } @@ -87,8 +86,6 @@ pub mod dev { } fn execute(&self, query: &str) -> QueryResult { - use diesel::debug_query; - debug_query::(&query); self.inner.execute(query) } @@ -99,8 +96,6 @@ pub mod dev { Self::Backend: HasSqlType, U: Queryable, { - use diesel::debug_query; - debug_query::(&source); self.inner.query_by_index(source) } @@ -109,8 +104,8 @@ pub mod dev { T: QueryFragment + QueryId, U: QueryableByName, { - use diesel::debug_query; - debug_query::(&source); + let q = debug_query::(&source).to_string(); + debug!("{:?}", q); self.inner.query_by_name(source) } @@ -118,8 +113,8 @@ pub mod dev { where T: QueryFragment + QueryId, { - use diesel::debug_query; - debug_query::(&source); + let q = debug_query::(&source).to_string(); + debug!("{:?}", q); self.inner.execute_returning_count(source) } diff --git a/jirs-server/src/main.rs b/jirs-server/src/main.rs index 319f389c..26432b6d 100644 --- a/jirs-server/src/main.rs +++ b/jirs-server/src/main.rs @@ -1,5 +1,7 @@ #[macro_use] extern crate diesel; +#[macro_use] +extern crate log; use actix_cors::Cors; use actix_web::{web, App, HttpServer}; @@ -13,8 +15,8 @@ pub mod schema; #[actix_rt::main] async fn main() -> Result<(), String> { - env_logger::init(); dotenv::dotenv().ok(); + pretty_env_logger::init(); let port = std::env::var("JIRS_SERVER_PORT").unwrap_or_else(|_| "3000".to_string()); let bind = std::env::var("JIRS_SERVER_BIND").unwrap_or_else(|_| "0.0.0.0".to_string());