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
This commit is contained in:
aecsocket
2025-12-12 02:09:57 +00:00
committed by GitHub
parent 91accd5578
commit 7aaf99a0c8
8 changed files with 71 additions and 14 deletions

15
Cargo.lock generated
View File

@@ -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",
]

View File

@@ -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" }

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1 @@
Service logging utilities.

View File

@@ -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::<OutputFormat>()
@@ -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<String> {
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)
}
}

View File

@@ -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]

View File

@@ -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};