feat: set max loglevel in logger configuration
This commit is contained in:
parent
bb16554097
commit
f6d1bfda7d
3 changed files with 32 additions and 9 deletions
|
@ -12,7 +12,7 @@ repository = "https://codeberg.org/Caznix/Zenyx"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = [ "subcrates/renderer","subcrates/zlog"]
|
members = ["subcrates/renderer", "subcrates/zlog"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
zlog = { path = "subcrates/zlog" }
|
zlog = { path = "subcrates/zlog" }
|
||||||
|
@ -46,5 +46,5 @@ tracing = "0.1.41"
|
||||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||||
vulkano = "0.35.1"
|
vulkano = "0.35.1"
|
||||||
wgpu = { version = "25.0.0", features = ["spirv"] }
|
wgpu = { version = "25.0.0", features = ["spirv"] }
|
||||||
winit = "0.30.9"
|
winit = { version = "0.30.9", features = ["android-native-activity"] }
|
||||||
zlog.workspace = true
|
zlog.workspace = true
|
||||||
|
|
|
@ -4,18 +4,19 @@ use crate::LogLevel;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct LoggerConfig {
|
pub struct LoggerConfig {
|
||||||
pub(crate) log_level: LogLevel,
|
pub(crate) log_level: Option<LogLevel>,
|
||||||
pub(crate) log_to_file: bool,
|
pub(crate) log_to_file: bool,
|
||||||
pub(crate) log_file_path: PathBuf,
|
pub(crate) log_file_path: PathBuf,
|
||||||
pub(crate) log_to_stdout: bool,
|
pub(crate) log_to_stdout: bool,
|
||||||
pub(crate) stdout_color: bool,
|
pub(crate) stdout_color: bool,
|
||||||
pub(crate) stdout_include_time: bool,
|
pub(crate) stdout_include_time: bool,
|
||||||
pub(crate) file_include_time: bool,
|
pub(crate) file_include_time: bool,
|
||||||
|
pub(crate) crate_max_level: Option<LogLevel>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LoggerConfig {
|
impl LoggerConfig {
|
||||||
pub fn level(mut self, level: LogLevel) -> Self {
|
pub fn level(mut self, level: LogLevel) -> Self {
|
||||||
self.log_level = level;
|
self.log_level = Some(level);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +54,8 @@ impl LoggerConfig {
|
||||||
impl Default for LoggerConfig {
|
impl Default for LoggerConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
log_level: LogLevel::Debug,
|
log_level: None,
|
||||||
|
crate_max_level: None,
|
||||||
log_to_file: true,
|
log_to_file: true,
|
||||||
log_file_path: "app.log".into(),
|
log_file_path: "app.log".into(),
|
||||||
log_to_stdout: true,
|
log_to_stdout: true,
|
||||||
|
|
|
@ -18,8 +18,9 @@ use std::{
|
||||||
|
|
||||||
use config::LoggerConfig;
|
use config::LoggerConfig;
|
||||||
use query::LogQuery;
|
use query::LogQuery;
|
||||||
use tracing::{Event, Level, Subscriber};
|
use tracing::{Event, Level, Subscriber, level_filters::LevelFilter, subscriber::DefaultGuard};
|
||||||
use tracing_subscriber::{
|
use tracing_subscriber::{
|
||||||
|
fmt::writer::WithFilter,
|
||||||
layer::{Context, Layer, SubscriberExt},
|
layer::{Context, Layer, SubscriberExt},
|
||||||
registry::LookupSpan,
|
registry::LookupSpan,
|
||||||
util::SubscriberInitExt,
|
util::SubscriberInitExt,
|
||||||
|
@ -153,6 +154,17 @@ impl From<LogLevel> for tracing::Level {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl From<LogLevel> for LevelFilter {
|
||||||
|
fn from(level: LogLevel) -> Self {
|
||||||
|
match level {
|
||||||
|
LogLevel::Error => LevelFilter::ERROR,
|
||||||
|
LogLevel::Info => LevelFilter::INFO,
|
||||||
|
LogLevel::Debug => LevelFilter::DEBUG,
|
||||||
|
LogLevel::Trace => LevelFilter::TRACE,
|
||||||
|
LogLevel::Warn => LevelFilter::WARN,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for LogLevel {
|
impl fmt::Display for LogLevel {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -238,13 +250,22 @@ impl Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer_layer = BufferLayer::new(log_entries.clone(), senders);
|
let buffer_layer = BufferLayer::new(log_entries.clone(), senders);
|
||||||
let subscriber = tracing_subscriber::registry().with(buffer_layer);
|
|
||||||
let guard = subscriber.set_default();
|
|
||||||
|
|
||||||
Logger {
|
let mk_logger = |guard: DefaultGuard| Logger {
|
||||||
subscriber_guard: Some(guard),
|
subscriber_guard: Some(guard),
|
||||||
log_entries,
|
log_entries,
|
||||||
_handles: handles,
|
_handles: handles,
|
||||||
|
};
|
||||||
|
if let Some(level) = config.log_level {
|
||||||
|
let subscriber = tracing_subscriber::registry()
|
||||||
|
.with(buffer_layer)
|
||||||
|
.with(LevelFilter::from(level));
|
||||||
|
let guard = subscriber.set_default();
|
||||||
|
mk_logger(guard)
|
||||||
|
} else {
|
||||||
|
let subscriber = tracing_subscriber::registry().with(buffer_layer);
|
||||||
|
let guard = subscriber.set_default();
|
||||||
|
mk_logger(guard)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue