From 0773347daf9dd5b1433884aeabd007f2f605adeb Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 1 Feb 2020 07:11:14 -0500 Subject: logging setup --- Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 4 +++- src/lib.rs | 7 +------ src/main.rs | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ff6b8e..c39255e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,16 @@ dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "colored" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cookie" version = "0.11.2" @@ -307,6 +317,16 @@ name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fern" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "colored 1.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "filetime" version = "0.2.8" @@ -1003,6 +1023,8 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1889,6 +1911,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum colored 1.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8815e2ab78f3a59928fc32e141fbeece88320a240e43f47b2fd64ea3a88a5b3d" "checksum cookie 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d9fac5e7bdefb6160fb181ee0eaa6f96704b625c70e6d61c465cb35750a4ea12" "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" @@ -1902,6 +1925,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" "checksum encoding_rs 0.8.22 (registry+https://github.com/rust-lang/crates.io-index)" = "cd8d03faa7fe0c1431609dfad7bbe827af30f82e1e2ae6f7ee4fca6bd764bc28" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +"checksum fern 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e69ab0d5aca163e388c3a49d284fed6c3d0810700e77c5ae2756a50ec1a4daaa" "checksum filetime 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" diff --git a/Cargo.toml b/Cargo.toml index 4b4dc3c..848585b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,6 @@ dotenv = "0.9.0" bcrypt = "0.6" base64 = "0.11" rand = "0.7" -chrono = { version = "0.4", features = ["serde"] } \ No newline at end of file +chrono = { version = "0.4", features = ["serde"] } +log = "0.4" +fern = { version = "0.5", features = ["colored"] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 60d89cd..5d3ff06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,12 +9,7 @@ extern crate rocket_contrib; #[macro_use] extern crate diesel; -#[derive(Debug)] -pub struct Config { - pub client_id: String, - pub client_secret: String, - pub base_url: String, -} +extern crate fern; pub mod db; pub mod error; diff --git a/src/main.rs b/src/main.rs index d8d1283..6e11fc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +extern crate fern; +#[macro_use] +extern crate log; extern crate clap; use clap::App; use clap::Arg; @@ -5,6 +8,21 @@ use clap::SubCommand; use diesel::connection::Connection; use diesel::pg::PgConnection; +fn setup_logger() -> Result<(), fern::InitError> { + use fern::colors::ColoredLevelConfig; + let colors = ColoredLevelConfig::new(); + fern::Dispatch::new() + .format(move |out, message, record| { + out.finish(format_args!("[{}] {}", + colors.color(record.level()), + message)) + }) + .level(log::LevelFilter::Info) + .chain(std::io::stdout()) + .apply()?; + Ok(()) +} + fn main() { let matches = App::new("pjournal") .version("0.1") @@ -56,6 +74,8 @@ fn main() { ) .get_matches(); + setup_logger().expect("logger"); + let base_url = matches .value_of("base_url") .unwrap_or("http://localhost:8000"); @@ -80,6 +100,7 @@ fn main() { let password = matches.value_of("PASSWORD").unwrap(); pjournal::db::adduser(&conn, user, password).unwrap(); } else { + info!("Start server"); pjournal::server::start(conn, db_url, base_url); } } -- cgit v1.2.3