Skip to content

Commit 42912a0

Browse files
committed
better log configuration
Signed-off-by: Rachel Powers <[email protected]>
1 parent 6e5038c commit 42912a0

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

mcmeta/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ serde_valid = "0.15.0"
2121
tokio = { version = "1.27.0", features = ["full"] }
2222
tracing = "0.1.37"
2323
tracing-appender = "0.2.2"
24-
tracing-subscriber = { version = "0.3.16" }
24+
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

mcmeta/src/app_config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,33 @@ pub struct MetadataConfig {
1414
pub max_parallel_fetch_connections: usize,
1515
}
1616

17+
#[derive(Deserialize, Debug)]
18+
pub struct DebugLogConfig {
19+
pub enable: bool,
20+
pub path: String,
21+
pub prefix: String,
22+
pub level: String,
23+
}
24+
25+
impl DebugLogConfig {
26+
pub fn tracing_level(&self) -> tracing::Level {
27+
match self.level.to_uppercase().trim() {
28+
"TRACE" => tracing::Level::TRACE,
29+
"DEBUG" => tracing::Level::DEBUG,
30+
"INFO" => tracing::Level::INFO,
31+
"WARN" => tracing::Level::WARN,
32+
"ERROR" => tracing::Level::ERROR,
33+
_ => tracing::Level::ERROR,
34+
}
35+
}
36+
}
37+
1738
#[derive(Deserialize, Debug)]
1839
pub struct ServerConfig {
1940
pub bind_address: String,
2041
pub storage_format: StorageFormat,
2142
pub metadata: MetadataConfig,
43+
pub debug_log: DebugLogConfig,
2244
}
2345

2446
impl ServerConfig {
@@ -28,6 +50,10 @@ impl ServerConfig {
2850
.set_default("storage_format.type", "json")?
2951
.set_default("storage_format.meta_directory", "meta")?
3052
.set_default("metadata.max_parallel_fetch_connections", 4)?
53+
.set_default("debug_log.enable", false)?
54+
.set_default("debug_log.path", "./logs")?
55+
.set_default("debug_log.prefix", "mcmeta.log")?
56+
.set_default("debug_log.level", "debug")?
3157
.add_source(config::File::new(
3258
"config/settings",
3359
config::FileFormat::Json,

mcmeta/src/main.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ use tracing_subscriber::{filter, prelude::*};
254254

255255
#[tokio::main]
256256
async fn main() -> Result<(), MetaMCError> {
257-
let file_appender = tracing_appender::rolling::hourly("logs", "mcmeta.log");
257+
let config = Arc::new(ServerConfig::from_config()?);
258+
259+
let file_appender = tracing_appender::rolling::hourly(&config.debug_log.path, &config.debug_log.prefix);
258260
let (non_blocking_file, _guard) = tracing_appender::non_blocking(file_appender);
259261
let stdout_log = tracing_subscriber::fmt::layer()
260262
.with_file(true)
@@ -263,18 +265,21 @@ async fn main() -> Result<(), MetaMCError> {
263265
.compact();
264266

265267
let debug_log = tracing_subscriber::fmt::layer()
268+
.with_ansi(false)
266269
.with_writer(non_blocking_file)
267-
.with_filter(filter::LevelFilter::DEBUG);
270+
.with_filter(filter::LevelFilter::from_level(config.debug_log.tracing_level()));
268271

269-
tracing_subscriber::registry()
270-
.with(
271-
stdout_log
272-
// .with_filter(filter::LevelFilter::INFO)
273-
.and_then(debug_log),
274-
)
275-
.init();
272+
if config.debug_log.enable {
273+
tracing_subscriber::registry()
274+
.with(stdout_log.with_filter(filter::EnvFilter::from_default_env()))
275+
.with(debug_log)
276+
.init();
277+
} else {
278+
tracing_subscriber::registry()
279+
.with(stdout_log.with_filter(filter::EnvFilter::from_default_env()))
280+
.init();
281+
}
276282

277-
let config = Arc::new(ServerConfig::from_config()?);
278283
debug!("Config: {:#?}", config);
279284

280285
config

0 commit comments

Comments
 (0)