feat(test): log for integration tests
- log for test are configurable to either be spit into the void (cargo test default) or into `stdout`.
This commit is contained in:
parent
70a4eb23c6
commit
9336235b64
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -613,6 +613,7 @@ dependencies = [
|
|||||||
"actix-web",
|
"actix-web",
|
||||||
"chrono",
|
"chrono",
|
||||||
"config",
|
"config",
|
||||||
|
"once_cell",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
@ -26,6 +26,7 @@ tracing = { version = "0.1.40", features = ["log"] }
|
|||||||
tracing-subscriber = { version = "0.3.18", features = ["registry", "env-filter"] }
|
tracing-subscriber = { version = "0.3.18", features = ["registry", "env-filter"] }
|
||||||
tracing-bunyan-formatter = "0.3.9"
|
tracing-bunyan-formatter = "0.3.9"
|
||||||
tracing-log = "0.2.0"
|
tracing-log = "0.2.0"
|
||||||
|
once_cell = "1.19.0"
|
||||||
|
|
||||||
[dependencies.sqlx]
|
[dependencies.sqlx]
|
||||||
version = "0.7"
|
version = "0.7"
|
||||||
|
@ -8,7 +8,11 @@ use sqlx::PgPool;
|
|||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let configuration = get_configuration().expect("Failed to read configuration");
|
let configuration = get_configuration().expect("Failed to read configuration");
|
||||||
|
|
||||||
let subscriber = get_subscriber("email_newsletter_api".into(), "info".into());
|
let subscriber = get_subscriber(
|
||||||
|
"email_newsletter_api".into(),
|
||||||
|
"info".into(),
|
||||||
|
std::io::stdout,
|
||||||
|
);
|
||||||
init_subscriber(subscriber);
|
init_subscriber(subscriber);
|
||||||
|
|
||||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||||
|
@ -2,12 +2,19 @@ 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::{layer::SubscriberExt, EnvFilter, Registry};
|
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt, EnvFilter, Registry};
|
||||||
|
|
||||||
pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Send + Sync {
|
pub fn get_subscriber<Sink>(
|
||||||
|
name: String,
|
||||||
|
env_filter: String,
|
||||||
|
sink: Sink,
|
||||||
|
) -> impl Subscriber + Send + Sync
|
||||||
|
where
|
||||||
|
Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static,
|
||||||
|
{
|
||||||
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)
|
||||||
@ -15,6 +22,9 @@ pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sen
|
|||||||
}
|
}
|
||||||
|
|
||||||
// init_subscriber should only be called once
|
// init_subscriber should only be called once
|
||||||
|
//
|
||||||
|
// This is solved with the once_cell crate
|
||||||
|
// until the std::sync::SyncOnceCell is stable in the toolchain
|
||||||
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
||||||
LogTracer::init().expect("Failed to set logger");
|
LogTracer::init().expect("Failed to set logger");
|
||||||
set_global_default(subscriber).expect("Failed to set subscriber");
|
set_global_default(subscriber).expect("Failed to set subscriber");
|
||||||
|
@ -1,8 +1,22 @@
|
|||||||
use email_newsletter_api::configuration::{get_configuration, DatabaseSettings};
|
use email_newsletter_api::{
|
||||||
|
configuration::{get_configuration, DatabaseSettings},
|
||||||
|
telemetry::{get_subscriber, init_subscriber},
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
|
||||||
|
static TRACING: Lazy<()> = Lazy::new(|| {
|
||||||
|
if std::env::var("TEST_LOG").is_ok() {
|
||||||
|
let subscriber = get_subscriber("test".into(), "info".into(), std::io::stdout);
|
||||||
|
init_subscriber(subscriber);
|
||||||
|
} else {
|
||||||
|
let subscriber = get_subscriber("test".into(), "debug".into(), std::io::sink);
|
||||||
|
init_subscriber(subscriber);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
pub struct TestApp {
|
pub struct TestApp {
|
||||||
pub address: String,
|
pub address: String,
|
||||||
pub db_pool: PgPool,
|
pub db_pool: PgPool,
|
||||||
@ -10,6 +24,8 @@ pub struct TestApp {
|
|||||||
|
|
||||||
#[allow(clippy::let_underscore_future)]
|
#[allow(clippy::let_underscore_future)]
|
||||||
pub async fn spawn_app() -> TestApp {
|
pub async fn spawn_app() -> TestApp {
|
||||||
|
Lazy::force(&TRACING);
|
||||||
|
|
||||||
/* Spawn a app server with a TcpListener bound to localhost:<random port>
|
/* Spawn a app server with a TcpListener bound to localhost:<random port>
|
||||||
*
|
*
|
||||||
* Returns a valid IPv4 string (i.e localhost:8080)
|
* Returns a valid IPv4 string (i.e localhost:8080)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user