Changes to allow more dynamic logging

Logs in much greater detail using the tracing packages and formatted using Bunyan. Advised to install bunyan on machine for more pretty logs over raw json.
This commit is contained in:
Nick Bland
2021-11-16 13:50:32 +10:00
parent a746b45f78
commit 1f06f5e66f
8 changed files with 252 additions and 15 deletions

View File

@@ -1,17 +1,33 @@
use std::net::TcpListener;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
use once_cell::sync::Lazy;
use mail_app::startup::run;
use mail_app::configuration::{get_configuration, DatabaseSettings};
use mail_app::telemetry::{get_subscriber, init_subscriber};
pub struct TestApp {
pub address: String,
pub db_pool: PgPool,
}
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);
};
});
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
async fn spawn_app() -> TestApp {
Lazy::force(&TRACING);
let listener = TcpListener::bind("127.0.0.1:0")
.expect("Failed to bind to random port");
let port = listener.local_addr().unwrap().port();
@@ -20,7 +36,6 @@ async fn spawn_app() -> TestApp {
let mut configuration = get_configuration()
.expect("Failed to read configuration.");
configuration.database.database_name = Uuid::new_v4().to_string(); // Adjust database string to be random!
let connection_pool = configure_database(&configuration.database).await;
let server = run(listener, connection_pool.clone())