You've already forked AstralRinth
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
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 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)
|
||||
}
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
@@ -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};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user