Update to use once_cell instead of lazy_static.

Use a custom sink instead of different log levels to silence logs in tests.
This commit is contained in:
LukeMathWalker 2021-05-01 15:11:11 +01:00
parent 2e690fa838
commit bcb108a2fd
5 changed files with 532 additions and 352 deletions

846
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -13,7 +13,10 @@ path = "src/main.rs"
name = "zero2prod" name = "zero2prod"
[dependencies] [dependencies]
actix-web = "4.0.0-beta.3" actix-http = "=3.0.0-beta.5"
actix-service = "=2.0.0-beta.5"
actix-web = "=4.0.0-beta.5"
serde = "1.0.115" serde = "1.0.115"
config = { version = "0.10.1", default-features = false, features = ["yaml"] } config = { version = "0.10.1", default-features = false, features = ["yaml"] }
sqlx = { version = "0.5.1", default-features = false, features = [ "runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] } sqlx = { version = "0.5.1", default-features = false, features = [ "runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
@ -31,7 +34,6 @@ unicode-segmentation = "1.7.1"
validator = "0.12.0" validator = "0.12.0"
[dev-dependencies] [dev-dependencies]
lazy_static = "1.4.0"
claim = "0.4.0" claim = "0.4.0"
quickcheck = "0.9.2" quickcheck = "0.9.2"
quickcheck_macros = "0.9.1" quickcheck_macros = "0.9.1"
@ -40,3 +42,5 @@ wiremock = "0.4.7"
serde_json = "1.0.61" serde_json = "1.0.61"
actix-rt = "2" actix-rt = "2"
tokio = { version = "1", features = ["macros"] } tokio = { version = "1", features = ["macros"] }
reqwest = { version = "0.11", features = ["json"] }
once_cell = "1.7.2"

View file

@ -7,7 +7,7 @@ use zero2prod::telemetry::{get_subscriber, init_subscriber};
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { 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); init_subscriber(subscriber);
let configuration = get_configuration().expect("Failed to read configuration."); let configuration = get_configuration().expect("Failed to read configuration.");

View file

@ -2,6 +2,7 @@ use tracing::subscriber::set_global_default;
use tracing::Subscriber; use tracing::Subscriber;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer; use tracing_log::LogTracer;
use tracing_subscriber::fmt::MakeWriter;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
/// Compose multiple layers into a `tracing`'s subscriber. /// 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 /// 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. /// 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 = let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(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() Registry::default()
.with(env_filter) .with(env_filter)
.with(JsonStorageLayer) .with(JsonStorageLayer)

View file

@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use sqlx::{Connection, Executor, PgConnection, PgPool}; use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener; use std::net::TcpListener;
use uuid::Uuid; use uuid::Uuid;
@ -6,14 +7,18 @@ use zero2prod::email_client::EmailClient;
use zero2prod::startup::run; use zero2prod::startup::run;
use zero2prod::telemetry::{get_subscriber, init_subscriber}; use zero2prod::telemetry::{get_subscriber, init_subscriber};
// Ensure that the `tracing` stack is only initialised once using `lazy_static` // Ensure that the `tracing` stack is only initialised once using `once_cell`
lazy_static::lazy_static! { static TRACING: Lazy<()> = Lazy::new(|| {
static ref TRACING: () = { let default_filter_level = "info".to_string();
let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" }; let subscriber_name = "test".to_string();
let subscriber = get_subscriber("test".into(), filter.into()); 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); init_subscriber(subscriber);
}; };
} });
pub struct TestApp { pub struct TestApp {
pub address: String, pub address: String,
@ -23,7 +28,7 @@ pub struct TestApp {
async fn spawn_app() -> TestApp { async fn spawn_app() -> TestApp {
// The first time `initialize` is invoked the code in `TRACING` is executed. // The first time `initialize` is invoked the code in `TRACING` is executed.
// All other invocations will instead skip execution. // 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"); 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 // We retrieve the port assigned to us by the OS