You've already forked pages
forked from didirus/AstralRinth
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:
15
Cargo.lock
generated
15
Cargo.lock
generated
@@ -5056,6 +5056,17 @@ dependencies = [
|
|||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "modrinth-log"
|
||||||
|
version = "0.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"dotenvy",
|
||||||
|
"eyre",
|
||||||
|
"tracing",
|
||||||
|
"tracing-ecs",
|
||||||
|
"tracing-subscriber",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "modrinth-maxmind"
|
name = "modrinth-maxmind"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
@@ -5082,12 +5093,10 @@ dependencies = [
|
|||||||
"derive_more 2.0.1",
|
"derive_more 2.0.1",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"eyre",
|
"eyre",
|
||||||
|
"modrinth-log",
|
||||||
"rust_decimal",
|
"rust_decimal",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tracing",
|
|
||||||
"tracing-ecs",
|
|
||||||
"tracing-subscriber",
|
|
||||||
"utoipa",
|
"utoipa",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ members = [
|
|||||||
"packages/app-lib",
|
"packages/app-lib",
|
||||||
"packages/ariadne",
|
"packages/ariadne",
|
||||||
"packages/daedalus",
|
"packages/daedalus",
|
||||||
|
"packages/modrinth-log",
|
||||||
"packages/modrinth-maxmind",
|
"packages/modrinth-maxmind",
|
||||||
"packages/modrinth-util",
|
"packages/modrinth-util",
|
||||||
"packages/path-util",
|
"packages/path-util",
|
||||||
@@ -107,6 +108,7 @@ lettre = { version = "0.11.19", default-features = false, features = [
|
|||||||
] }
|
] }
|
||||||
maxminddb = "0.26.0"
|
maxminddb = "0.26.0"
|
||||||
meilisearch-sdk = { version = "0.30.0", default-features = false }
|
meilisearch-sdk = { version = "0.30.0", default-features = false }
|
||||||
|
modrinth-log = { path = "packages/modrinth-log" }
|
||||||
modrinth-maxmind = { path = "packages/modrinth-maxmind" }
|
modrinth-maxmind = { path = "packages/modrinth-maxmind" }
|
||||||
modrinth-util = { path = "packages/modrinth-util" }
|
modrinth-util = { path = "packages/modrinth-util" }
|
||||||
muralpay = { path = "packages/muralpay" }
|
muralpay = { path = "packages/muralpay" }
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
MEILI_MASTER_KEY: modrinth
|
MEILI_MASTER_KEY: modrinth
|
||||||
MEILI_HTTP_PAYLOAD_SIZE_LIMIT: 107374182400
|
MEILI_HTTP_PAYLOAD_SIZE_LIMIT: 107374182400
|
||||||
|
MEILI_LOG_LEVEL: warn
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ['CMD', 'curl', '--fail', 'http://localhost:7700/health']
|
test: ['CMD', 'curl', '--fail', 'http://localhost:7700/health']
|
||||||
interval: 3s
|
interval: 3s
|
||||||
|
|||||||
15
packages/modrinth-log/Cargo.toml
Normal file
15
packages/modrinth-log/Cargo.toml
Normal 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
|
||||||
1
packages/modrinth-log/README.md
Normal file
1
packages/modrinth-log/README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Service logging utilities.
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
//! Service logging utilities.
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use eyre::{Result, eyre};
|
use eyre::{Result, WrapErr, eyre};
|
||||||
use tracing::level_filters::LevelFilter;
|
use tracing::level_filters::LevelFilter;
|
||||||
use tracing_ecs::ECSLayerBuilder;
|
use tracing_ecs::ECSLayerBuilder;
|
||||||
use tracing_subscriber::{
|
use tracing_subscriber::{
|
||||||
EnvFilter, layer::SubscriberExt, util::SubscriberInitExt,
|
EnvFilter, layer::SubscriberExt, util::SubscriberInitExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Context, env_var};
|
|
||||||
|
|
||||||
/// How this service will output logs to the terminal output.
|
/// How this service will output logs to the terminal output.
|
||||||
///
|
///
|
||||||
/// See [`init`].
|
/// See [`init`].
|
||||||
@@ -47,6 +45,21 @@ pub const OUTPUT_FORMAT_ENV_VAR: &str = "MODRINTH_OUTPUT_FORMAT";
|
|||||||
///
|
///
|
||||||
/// Errors if logging could not be initialized.
|
/// Errors if logging could not be initialized.
|
||||||
pub fn init() -> Result<()> {
|
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) {
|
let output_format = match env_var(OUTPUT_FORMAT_ENV_VAR) {
|
||||||
Ok(format) => format
|
Ok(format) => format
|
||||||
.parse::<OutputFormat>()
|
.parse::<OutputFormat>()
|
||||||
@@ -58,12 +71,20 @@ pub fn init() -> Result<()> {
|
|||||||
.with_default_directive(LevelFilter::INFO.into())
|
.with_default_directive(LevelFilter::INFO.into())
|
||||||
.from_env_lossy();
|
.from_env_lossy();
|
||||||
|
|
||||||
let result = match output_format {
|
let result = match (output_format, compact) {
|
||||||
OutputFormat::Human => tracing_subscriber::registry()
|
(OutputFormat::Human, false) => tracing_subscriber::registry()
|
||||||
.with(env_filter)
|
.with(env_filter)
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.try_init(),
|
.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(env_filter)
|
||||||
.with(ECSLayerBuilder::default().stdout())
|
.with(ECSLayerBuilder::default().stdout())
|
||||||
.try_init(),
|
.try_init(),
|
||||||
@@ -72,3 +93,13 @@ pub fn init() -> Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,11 +9,9 @@ actix-web = { workspace = true }
|
|||||||
derive_more = { workspace = true, features = ["display", "error", "from"] }
|
derive_more = { workspace = true, features = ["display", "error", "from"] }
|
||||||
dotenvy = { workspace = true }
|
dotenvy = { workspace = true }
|
||||||
eyre = { workspace = true }
|
eyre = { workspace = true }
|
||||||
|
modrinth-log = { workspace = true }
|
||||||
rust_decimal = { workspace = true, features = ["macros"], optional = true }
|
rust_decimal = { workspace = true, features = ["macros"], optional = true }
|
||||||
serde = { workspace = true, features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
tracing = { workspace = true }
|
|
||||||
tracing-ecs = { workspace = true }
|
|
||||||
tracing-subscriber = { workspace = true }
|
|
||||||
utoipa = { workspace = true, optional = true }
|
utoipa = { workspace = true, optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
pub mod log;
|
|
||||||
|
|
||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
pub mod decimal;
|
pub mod decimal;
|
||||||
|
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
|
pub use modrinth_log as log;
|
||||||
|
|
||||||
use eyre::{Result, eyre};
|
use eyre::{Result, eyre};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user