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:
@@ -8,7 +8,11 @@ use sqlx::PgPool;
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
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);
|
||||
|
||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||
|
@@ -2,12 +2,19 @@ use tracing::subscriber::set_global_default;
|
||||
use tracing::Subscriber;
|
||||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
||||
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 =
|
||||
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)
|
||||
@@ -15,6 +22,9 @@ pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sen
|
||||
}
|
||||
|
||||
// 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) {
|
||||
LogTracer::init().expect("Failed to set logger");
|
||||
set_global_default(subscriber).expect("Failed to set subscriber");
|
||||
|
Reference in New Issue
Block a user