Skip to content

Commit

Permalink
refactor: move init_logging to utils crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekhorn committed Jan 16, 2024
1 parent e2f0224 commit 48acc93
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion srcs/services/item_producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ clap.workspace = true
prost.workspace = true
serde.workspace = true
serde_json.workspace = true
socketioxide = { version = "0.10", features = ["tracing", "extensions", "state"] }
socketioxide = { version = "0.10", features = [
"tracing",
"extensions",
"state",
] }
sqlx.workspace = true
tokio.workspace = true
tower.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
futures.workspace = true
futures-util.workspace = true
utils = { path = "../../utils" }

[dev-dependencies]
rust_socketio = { version = "0.4.4", features = ["async"] }
17 changes: 2 additions & 15 deletions srcs/services/item_producer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use socketioxide::{
use sqlx::PgPool;
use tokio::net::TcpListener;
use tower::ServiceBuilder;
use tracing::{error, info, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;
use tracing::{error, info};

mod clients;
mod consumer;
Expand Down Expand Up @@ -50,7 +49,7 @@ struct Args {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
init_logging();
utils::init_logging();

let args = Args::parse();
let db_pool = PgPool::connect(&args.database_host).await?;
Expand Down Expand Up @@ -114,18 +113,6 @@ async fn app(db_pool: PgPool, shared_amqp_channel: Arc<Channel>) -> anyhow::Resu
Ok(Router::new().layer(ServiceBuilder::new().layer(io_layer)))
}

fn init_logging() {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

tracing_subscriber::fmt()
.with_target(true)
.with_level(true)
.with_env_filter(env_filter)
.init();
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 1 addition & 14 deletions srcs/services/user_service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use sqlx::PgPool;
use tokio::net::TcpListener;
use tower_http::trace::TraceLayer;
use tracing::info;
use tracing_subscriber::{filter::LevelFilter, EnvFilter};

mod auth;
mod handlers;
Expand All @@ -23,7 +22,7 @@ struct Args {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
init_logging();
utils::init_logging();

let args = Args::parse();

Expand All @@ -45,15 +44,3 @@ async fn main() -> anyhow::Result<()> {

Ok(())
}

fn init_logging() {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

tracing_subscriber::fmt()
.with_target(true)
.with_level(true)
.with_env_filter(env_filter)
.init();
}
2 changes: 2 additions & 0 deletions srcs/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ crate-type = ["lib"]
axum = { workspace = true, features = ["macros"] }
serde.workspace = true
serde_json.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
15 changes: 15 additions & 0 deletions srcs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use axum::{
use serde::Serialize;
use serde_json::json;

use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;

#[derive(FromRequest)]
#[from_request(via(axum::Json), rejection(ApiError))]
pub struct Json<T>(pub T);
Expand Down Expand Up @@ -38,3 +41,15 @@ impl From<JsonRejection> for ApiError {
Self(rejection.status(), Some(rejection.body_text()))
}
}

pub fn init_logging() {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

tracing_subscriber::fmt()
.with_target(true)
.with_level(true)
.with_env_filter(env_filter)
.init();
}

0 comments on commit 48acc93

Please sign in to comment.