From 7aaf99a0c852bd6cd8d276822f9b014bf7f26cb4 Mon Sep 17 00:00:00 2001 From: aecsocket Date: Fri, 12 Dec 2025 02:09:57 +0000 Subject: [PATCH] Split logging utils into its own crate (#4852) * Split logging utils into its own crate * fix review comments * remove anyhow from root * make meilisearch less verbose --- Cargo.lock | 15 +++++-- Cargo.toml | 2 + docker-compose.yml | 1 + packages/modrinth-log/Cargo.toml | 15 +++++++ packages/modrinth-log/README.md | 1 + .../src/log.rs => modrinth-log/src/lib.rs} | 45 ++++++++++++++++--- packages/modrinth-util/Cargo.toml | 4 +- packages/modrinth-util/src/lib.rs | 2 +- 8 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 packages/modrinth-log/Cargo.toml create mode 100644 packages/modrinth-log/README.md rename packages/{modrinth-util/src/log.rs => modrinth-log/src/lib.rs} (59%) diff --git a/Cargo.lock b/Cargo.lock index de0e6baa..92cc9f38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5056,6 +5056,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "modrinth-log" +version = "0.0.0" +dependencies = [ + "dotenvy", + "eyre", + "tracing", + "tracing-ecs", + "tracing-subscriber", +] + [[package]] name = "modrinth-maxmind" version = "0.0.0" @@ -5082,12 +5093,10 @@ dependencies = [ "derive_more 2.0.1", "dotenvy", "eyre", + "modrinth-log", "rust_decimal", "serde", "serde_json", - "tracing", - "tracing-ecs", - "tracing-subscriber", "utoipa", ] diff --git a/Cargo.toml b/Cargo.toml index c6afc79c..089eb20d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "packages/app-lib", "packages/ariadne", "packages/daedalus", + "packages/modrinth-log", "packages/modrinth-maxmind", "packages/modrinth-util", "packages/path-util", @@ -107,6 +108,7 @@ lettre = { version = "0.11.19", default-features = false, features = [ ] } maxminddb = "0.26.0" meilisearch-sdk = { version = "0.30.0", default-features = false } +modrinth-log = { path = "packages/modrinth-log" } modrinth-maxmind = { path = "packages/modrinth-maxmind" } modrinth-util = { path = "packages/modrinth-util" } muralpay = { path = "packages/muralpay" } diff --git a/docker-compose.yml b/docker-compose.yml index dd1890a4..8ec36039 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,6 +28,7 @@ services: environment: MEILI_MASTER_KEY: modrinth MEILI_HTTP_PAYLOAD_SIZE_LIMIT: 107374182400 + MEILI_LOG_LEVEL: warn healthcheck: test: ['CMD', 'curl', '--fail', 'http://localhost:7700/health'] interval: 3s diff --git a/packages/modrinth-log/Cargo.toml b/packages/modrinth-log/Cargo.toml new file mode 100644 index 00000000..83ce980d --- /dev/null +++ b/packages/modrinth-log/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "modrinth-log" +edition.workspace = true +rust-version.workspace = true +repository.workspace = true + +[dependencies] +dotenvy = { workspace = true } +eyre = { workspace = true } +tracing = { workspace = true } +tracing-ecs = { workspace = true } +tracing-subscriber = { workspace = true } + +[lints] +workspace = true diff --git a/packages/modrinth-log/README.md b/packages/modrinth-log/README.md new file mode 100644 index 00000000..7827d91b --- /dev/null +++ b/packages/modrinth-log/README.md @@ -0,0 +1 @@ +Service logging utilities. diff --git a/packages/modrinth-util/src/log.rs b/packages/modrinth-log/src/lib.rs similarity index 59% rename from packages/modrinth-util/src/log.rs rename to packages/modrinth-log/src/lib.rs index 0cd80687..316377d5 100644 --- a/packages/modrinth-util/src/log.rs +++ b/packages/modrinth-log/src/lib.rs @@ -1,16 +1,14 @@ -//! Service logging utilities. +#![doc = include_str!("../README.md")] use std::str::FromStr; -use eyre::{Result, eyre}; +use eyre::{Result, WrapErr, eyre}; use tracing::level_filters::LevelFilter; use tracing_ecs::ECSLayerBuilder; use tracing_subscriber::{ EnvFilter, layer::SubscriberExt, util::SubscriberInitExt, }; -use crate::{Context, env_var}; - /// How this service will output logs to the terminal output. /// /// See [`init`]. @@ -47,6 +45,21 @@ pub const OUTPUT_FORMAT_ENV_VAR: &str = "MODRINTH_OUTPUT_FORMAT"; /// /// Errors if logging could not be initialized. pub fn init() -> Result<()> { + init_with_config(false) +} + +/// Initializes logging for Modrinth services. +/// +/// This uses [`OUTPUT_FORMAT_ENV_VAR`] to determine the [`OutputFormat`] to +/// use - see that type for details of each possible format. +/// +/// - `compact`: if using [`OutputFormat::Human`], logs will not show timestamps +/// or target details. +/// +/// # Errors +/// +/// Errors if logging could not be initialized. +pub fn init_with_config(compact: bool) -> Result<()> { let output_format = match env_var(OUTPUT_FORMAT_ENV_VAR) { Ok(format) => format .parse::() @@ -58,12 +71,20 @@ pub fn init() -> Result<()> { .with_default_directive(LevelFilter::INFO.into()) .from_env_lossy(); - let result = match output_format { - OutputFormat::Human => tracing_subscriber::registry() + let result = match (output_format, compact) { + (OutputFormat::Human, false) => tracing_subscriber::registry() .with(env_filter) .with(tracing_subscriber::fmt::layer()) .try_init(), - OutputFormat::Json => tracing_subscriber::registry() + (OutputFormat::Human, true) => tracing_subscriber::registry() + .with(env_filter) + .with( + tracing_subscriber::fmt::layer() + .without_time() + .with_target(false), + ) + .try_init(), + (OutputFormat::Json, _) => tracing_subscriber::registry() .with(env_filter) .with(ECSLayerBuilder::default().stdout()) .try_init(), @@ -72,3 +93,13 @@ pub fn init() -> Result<()> { Ok(()) } + +fn env_var(key: &str) -> Result { + let value = dotenvy::var(key) + .wrap_err_with(|| eyre!("missing environment variable `{key}`"))?; + if value.is_empty() { + Err(eyre!("environment variable `{key}` is empty")) + } else { + Ok(value) + } +} diff --git a/packages/modrinth-util/Cargo.toml b/packages/modrinth-util/Cargo.toml index 1fa164f5..b461dc28 100644 --- a/packages/modrinth-util/Cargo.toml +++ b/packages/modrinth-util/Cargo.toml @@ -9,11 +9,9 @@ actix-web = { workspace = true } derive_more = { workspace = true, features = ["display", "error", "from"] } dotenvy = { workspace = true } eyre = { workspace = true } +modrinth-log = { workspace = true } rust_decimal = { workspace = true, features = ["macros"], optional = true } serde = { workspace = true, features = ["derive"] } -tracing = { workspace = true } -tracing-ecs = { workspace = true } -tracing-subscriber = { workspace = true } utoipa = { workspace = true, optional = true } [dev-dependencies] diff --git a/packages/modrinth-util/src/lib.rs b/packages/modrinth-util/src/lib.rs index e284f70c..e0502913 100644 --- a/packages/modrinth-util/src/lib.rs +++ b/packages/modrinth-util/src/lib.rs @@ -1,12 +1,12 @@ #![doc = include_str!("../README.md")] mod error; -pub mod log; #[cfg(feature = "decimal")] pub mod decimal; pub use error::*; +pub use modrinth_log as log; use eyre::{Result, eyre};