chore: some more config options

This commit is contained in:
Alexander Medvedev
2026-08-30 11:59:45 +02:00
parent 07be171b75
commit 544bfc21d5
2 changed files with 32 additions and 7 deletions

View File

@@ -8,12 +8,20 @@ use serde::{Deserialize, Serialize};
pub struct LoggingConfig { pub struct LoggingConfig {
/// Whether logging is enabled. /// Whether logging is enabled.
pub enabled: bool, pub enabled: bool,
/// Minimum log level for console and file output ("trace", "debug", "info", "warn", "error", "off").
pub level: String,
/// Whether to include thread names in log output. /// Whether to include thread names in log output.
pub threads: bool, pub threads: bool,
/// Whether to include thread IDs in log output.
pub thread_ids: bool,
/// Whether to include target (module/component path) in log output.
pub target: bool,
/// Whether to enable coloured log output. /// Whether to enable coloured log output.
pub color: bool, pub color: bool,
/// Whether to include timestamps in log entries. /// Whether to include timestamps in log entries.
pub timestamp: bool, pub timestamp: bool,
/// Format description for timestamps (using `time` format description syntax).
pub timestamp_format: String,
/// Path to the log file. /// Path to the log file.
pub file: String, pub file: String,
} }
@@ -22,9 +30,13 @@ impl Default for LoggingConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
enabled: true, enabled: true,
threads: true, level: "info".to_string(),
threads: false,
thread_ids: false,
target: false,
color: true, color: true,
timestamp: true, timestamp: true,
timestamp_format: "[hour]:[minute]:[second]".to_string(),
file: "latest.log".to_string(), file: "latest.log".to_string(),
} }
} }

View File

@@ -64,6 +64,8 @@ pub mod world;
pub struct LoggingConfig { pub struct LoggingConfig {
pub color: bool, pub color: bool,
pub threads: bool, pub threads: bool,
pub thread_ids: bool,
pub target: bool,
pub timestamp: bool, pub timestamp: bool,
} }
@@ -80,6 +82,7 @@ pub fn init_logger(advanced_config: &AdvancedConfiguration) {
let level = std::env::var("RUST_LOG") let level = std::env::var("RUST_LOG")
.ok() .ok()
.as_deref() .as_deref()
.or(Some(advanced_config.logging.level.as_str()))
.map(LevelFilter::from_str) .map(LevelFilter::from_str)
.and_then(Result::ok) .and_then(Result::ok)
.unwrap_or(LevelFilter::INFO); .unwrap_or(LevelFilter::INFO);
@@ -143,17 +146,25 @@ pub fn init_logger(advanced_config: &AdvancedConfiguration) {
.with_writer(std::sync::Mutex::new(logger)) .with_writer(std::sync::Mutex::new(logger))
.with_ansi(advanced_config.logging.color) .with_ansi(advanced_config.logging.color)
.with_ansi_sanitization(false) .with_ansi_sanitization(false)
.with_target(true) .with_target(advanced_config.logging.target)
.with_thread_names(advanced_config.logging.threads) .with_thread_names(advanced_config.logging.threads)
.with_thread_ids(advanced_config.logging.threads); .with_thread_ids(advanced_config.logging.thread_ids);
if advanced_config.logging.timestamp { if advanced_config.logging.timestamp {
let local_offset = let local_offset =
time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC); time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
let fmt_layer = fmt_layer.with_timer(fmt::time::OffsetTime::new( let format_str: &'static str = Box::leak(
local_offset, advanced_config
time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"), .logging
)); .timestamp_format
.clone()
.into_boxed_str(),
);
let timer_format = time::format_description::parse(format_str).unwrap_or_else(|_| {
time::macros::format_description!("[hour]:[minute]:[second]").to_vec()
});
let fmt_layer =
fmt_layer.with_timer(fmt::time::OffsetTime::new(local_offset, timer_format));
let registry = tracing_subscriber::registry() let registry = tracing_subscriber::registry()
.with(env_filter) .with(env_filter)
.with(fmt_layer); .with(fmt_layer);
@@ -177,6 +188,8 @@ pub fn init_logger(advanced_config: &AdvancedConfiguration) {
let logging_config = LoggingConfig { let logging_config = LoggingConfig {
color: advanced_config.logging.color, color: advanced_config.logging.color,
threads: advanced_config.logging.threads, threads: advanced_config.logging.threads,
thread_ids: advanced_config.logging.thread_ids,
target: advanced_config.logging.target,
timestamp: advanced_config.logging.timestamp, timestamp: advanced_config.logging.timestamp,
}; };