From 6f329557aff3c7810699bf1bafa0a073798e1b9f Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 10 Mar 2021 20:51:26 +0100 Subject: feat: add logging with slog --- Cargo.lock | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 5 +++ src/config.rs | 18 ++++++++ src/main.rs | 30 ++++++++++--- 4 files changed, 181 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4871eb..fb84b41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,6 +336,17 @@ dependencies = [ "syn", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -521,6 +532,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils 0.8.3", +] + [[package]] name = "crossbeam-queue" version = "0.2.3" @@ -528,7 +549,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" dependencies = [ "cfg-if 0.1.10", - "crossbeam-utils", + "crossbeam-utils 0.7.2", "maybe-uninit", ] @@ -543,6 +564,17 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "lazy_static", +] + [[package]] name = "crypto-mac" version = "0.9.1" @@ -603,6 +635,27 @@ dependencies = [ "generic-array", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if 1.0.0", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi 0.3.9", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -836,6 +889,17 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", +] + [[package]] name = "gico" version = "0.1.0" @@ -848,6 +912,9 @@ dependencies = [ "deadpool-postgres", "dotenv", "serde 1.0.124", + "slog", + "slog-async", + "slog-term", "tokio-pg-mapper", "tokio-pg-mapper-derive", "tokio-postgres", @@ -1432,7 +1499,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.16", "libc", "rand_chacha", "rand_core", @@ -1455,7 +1522,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.16", ] [[package]] @@ -1476,6 +1543,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom 0.2.2", + "redox_syscall", +] + [[package]] name = "regex" version = "1.4.3" @@ -1516,6 +1593,12 @@ version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" +[[package]] +name = "rustversion" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb5d2a036dc6d2d8fd16fde3498b04306e29bd193bf306a57427019b823d5acd" + [[package]] name = "ryu" version = "1.0.5" @@ -1639,6 +1722,37 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "slog" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06" + +[[package]] +name = "slog-async" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c60813879f820c85dbc4eabf3269befe374591289019775898d56a81a804fbdc" +dependencies = [ + "crossbeam-channel", + "slog", + "take_mut", + "thread_local", +] + +[[package]] +name = "slog-term" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95c1e7e5aab61ced6006149ea772770b84a0d16ce0f7885def313e4829946d76" +dependencies = [ + "atty", + "chrono", + "slog", + "term", + "thread_local", +] + [[package]] name = "smallvec" version = "1.6.1" @@ -1701,6 +1815,23 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi 0.3.9", +] + [[package]] name = "thread_local" version = "1.1.3" diff --git a/Cargo.toml b/Cargo.toml index 946f7fc..f0d9f2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,11 @@ actix-rt = "1.0.0" actix-web = "2.0.0" actix-http = "1.0.1" actix-service = "1.0.5" + +slog = "2.4.1" +slog-term = "2.5.0" +slog-async = "2.4.0" + dotenv = "0.15.0" config = "0.10.1" serde = { version = "1.0.104", features = ["derive"] } diff --git a/src/config.rs b/src/config.rs index 7146a0b..e6cd0bd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,9 @@ pub use config::ConfigError; +use deadpool_postgres::Pool; use serde::Deserialize; +use slog::{o, Drain, Logger}; +use slog_async; +use slog_term; #[derive(Deserialize)] pub struct ServerConfig { @@ -19,4 +23,18 @@ impl Config { cfg.merge(config::Environment::new())?; cfg.try_into() } + + pub fn logging() -> Logger { + let decorator = slog_term::TermDecorator::new().build(); + let drain = slog_term::FullFormat::new(decorator).build().fuse(); + let drain = slog_async::Async::new(drain).build().fuse(); + + slog::Logger::root(drain, o!()) + } +} + +#[derive(Clone)] +pub struct AppState { + pub pool: Pool, + pub log: slog::Logger, } diff --git a/src/main.rs b/src/main.rs index 51c2f6d..0b3d67f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ mod config; -use actix_web::{web, App, HttpResponse, HttpServer}; +use actix_web::{middleware, web, App, HttpServer}; use dotenv::dotenv; +use slog::info; use tokio_postgres::NoTls; -use crate::config::Config; +use crate::config::{Config, AppState}; + +async fn index(state: web::Data) -> &'static str { + info!(state.log, "GET `/` page"); + + "Hello from Rust!" +} #[actix_rt::main] async fn main() -> std::io::Result<()> { @@ -12,12 +19,23 @@ async fn main() -> std::io::Result<()> { let config = Config::from_env().unwrap(); let pool = config.pg.create_pool(NoTls).unwrap(); + let log = Config::logging(); + + info!( + log, + "Starting server at http://{}:{}", + config.server.host, + config.server.port + ); HttpServer::new(move || { - App::new().data(pool.clone()).service( - web::resource("/") - .to(|| HttpResponse::Ok().body("Hello from Rust!")), - ) + App::new() + .data(AppState { + pool: pool.clone(), + log: log.clone(), + }) + .wrap(middleware::Logger::default()) + .route("/", web::get().to(index)) }) .bind(format!("{}:{}", config.server.host, config.server.port))? .run() -- cgit v1.2.3-18-g5258