Add config option to enable json logging (#5471)

This commit is contained in:
Nutomic 2025-03-03 15:51:28 +00:00 committed by GitHub
parent e7ab5256f7
commit 97772b9553
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 4 deletions

View file

@ -36,7 +36,6 @@ codegen-units = 1 # Reduce parallel code generation.
debug = 0
[features]
json-log = ["tracing-subscriber/json"]
default = []
[workspace]
@ -107,7 +106,7 @@ actix-web = { version = "4.9.0", default-features = false, features = [
] }
tracing = { version = "0.1.41", default-features = false }
tracing-actix-web = { version = "0.7.15", default-features = false }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json"] }
url = { version = "2.5.4", features = ["serde"] }
reqwest = { version = "0.12.12", default-features = false, features = [
"blocking",

View file

@ -117,4 +117,6 @@
"lemmy.tld"
/* ... */
]
# Print logs in JSON format. You can also disable ANSI colors in logs with env var `NO_COLOR`.
json_logging: false
}

View file

@ -49,6 +49,8 @@ pub struct Settings {
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
#[doku(example = "lemmy.tld")]
cors_origin: Vec<String>,
/// Print logs in JSON format. You can also disable ANSI colors in logs with env var `NO_COLOR`.
pub json_logging: bool,
}
impl Settings {

View file

@ -1,6 +1,9 @@
use clap::Parser;
use lemmy_server::{start_lemmy_server, CmdArgs};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use lemmy_utils::{
error::{LemmyErrorType, LemmyResult},
settings::SETTINGS,
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
@ -11,7 +14,14 @@ pub async fn main() -> LemmyResult<()> {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::fmt().with_env_filter(filter).init();
if SETTINGS.json_logging {
tracing_subscriber::fmt()
.with_env_filter(filter)
.json()
.init();
} else {
tracing_subscriber::fmt().with_env_filter(filter).init();
}
let args = CmdArgs::parse();