summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-10 20:51:26 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-10 20:51:26 +0100
commit6f329557aff3c7810699bf1bafa0a073798e1b9f (patch)
tree256d152bd7161085264675851ca81d991917f0f2
parented24f917ffabfead27d8b5899e29b55eff2810cc (diff)
feat: add logging with slog
-rw-r--r--Cargo.lock137
-rw-r--r--Cargo.toml5
-rw-r--r--src/config.rs18
-rw-r--r--src/main.rs30
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
@@ -337,6 +337,17 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -522,13 +533,23 @@ dependencies = [
]
[[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"
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",
]
@@ -544,6 +565,17 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -604,6 +636,27 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -837,6 +890,17 @@ dependencies = [
]
[[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"
dependencies = [
@@ -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]]
@@ -1477,6 +1544,16 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1517,6 +1594,12 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1640,6 +1723,37 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1702,6 +1816,23 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
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<AppState>) -> &'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()