Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"packages/app-lib",
"packages/ariadne",
"packages/daedalus",
"packages/modrinth-log",
"packages/modrinth-maxmind",
"packages/modrinth-util",
"packages/path-util",
Expand Down Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions packages/modrinth-log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/modrinth-log/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Service logging utilities.
Original file line number Diff line number Diff line change
@@ -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`].
Expand Down Expand Up @@ -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>()
Expand All @@ -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(),
Expand All @@ -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)
}
}
4 changes: 1 addition & 3 deletions packages/modrinth-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion packages/modrinth-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down