From 8584ce6891403bff6c5ab292b32b25287b2a90e4 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 1 May 2021 15:11:11 +0100 Subject: [PATCH] Update to use once_cell instead of lazy_static. Use a custom sink instead of different log levels to silence logs in tests. --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/main.rs | 2 +- src/telemetry.rs | 9 +++++++-- tests/health_check.rs | 19 ++++++++++++------- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ab5174..53b7b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1172,9 +1172,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.5.2" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" [[package]] name = "opaque-debug" @@ -2508,7 +2508,7 @@ dependencies = [ "actix-web", "chrono", "config", - "lazy_static", + "once_cell", "reqwest", "serde", "sqlx", diff --git a/Cargo.toml b/Cargo.toml index 05fa370..d29498e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,6 @@ tracing-actix-web = "0.3.0-beta.2" [dev-dependencies] reqwest = { version = "0.11", features = ["json"] } -lazy_static = "1.4.0" actix-rt = "2" tokio = "1" +once_cell = "1.7.2" diff --git a/src/main.rs b/src/main.rs index 2f075a1..fa39aa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use zero2prod::telemetry::{get_subscriber, init_subscriber}; #[actix_web::main] async fn main() -> std::io::Result<()> { - let subscriber = get_subscriber("zero2prod".into(), "info".into()); + let subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout); init_subscriber(subscriber); let configuration = get_configuration().expect("Failed to read configuration."); diff --git a/src/telemetry.rs b/src/telemetry.rs index 27168fb..f8646a4 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -2,6 +2,7 @@ use tracing::subscriber::set_global_default; use tracing::Subscriber; use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_log::LogTracer; +use tracing_subscriber::fmt::MakeWriter; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; /// Compose multiple layers into a `tracing`'s subscriber. @@ -10,10 +11,14 @@ use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; /// /// We are using `impl Subscriber` as return type to avoid having to spell out the actual /// type of the returned subscriber, which is indeed quite complex. -pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sync + Send { +pub fn get_subscriber( + name: String, + env_filter: String, + sink: impl MakeWriter + Send + Sync + 'static, +) -> impl Subscriber + Sync + Send { let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter)); - let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout); + let formatting_layer = BunyanFormattingLayer::new(name, sink); Registry::default() .with(env_filter) .with(JsonStorageLayer) diff --git a/tests/health_check.rs b/tests/health_check.rs index 7aa46b8..3506c79 100644 --- a/tests/health_check.rs +++ b/tests/health_check.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use sqlx::{Connection, Executor, PgConnection, PgPool}; use std::net::TcpListener; use uuid::Uuid; @@ -5,14 +6,18 @@ use zero2prod::configuration::{get_configuration, DatabaseSettings}; use zero2prod::startup::run; use zero2prod::telemetry::{get_subscriber, init_subscriber}; -// Ensure that the `tracing` stack is only initialised once using `lazy_static` -lazy_static::lazy_static! { - static ref TRACING: () = { - let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" }; - let subscriber = get_subscriber("test".into(), filter.into()); +// Ensure that the `tracing` stack is only initialised once using `once_cell` +static TRACING: Lazy<()> = Lazy::new(|| { + let default_filter_level = "info".to_string(); + let subscriber_name = "test".to_string(); + if std::env::var("TEST_LOG").is_ok() { + let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout); + init_subscriber(subscriber); + } else { + let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink); init_subscriber(subscriber); }; -} +}); pub struct TestApp { pub address: String, @@ -22,7 +27,7 @@ pub struct TestApp { async fn spawn_app() -> TestApp { // The first time `initialize` is invoked the code in `TRACING` is executed. // All other invocations will instead skip execution. - lazy_static::initialize(&TRACING); + Lazy::force(&TRACING); let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); // We retrieve the port assigned to us by the OS