Skip to content

Commit

Permalink
feat: fixed feature flag in logger.rs and added some configuration fo…
Browse files Browse the repository at this point in the history
…r the log output
  • Loading branch information
thibault-cne committed Aug 1, 2024
1 parent a8a765e commit 0ef9038
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ log:
level: info
# Enable the metrics.
enable_metrics: true
# Enable thread ID in logs.
enable_thread_id: true
# Log on stderr.
stderr: false

# The keys and domains configuration
keys:
Expand Down
4 changes: 4 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ log:
level: debug
# Enable the metrics.
enable_metrics: true
# Enable thread ID in logs.
enable_thread_id: true
# Log on stderr.
stderr: false

keys:
key1:
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct LogConfig {
#[serde(deserialize_with = "de_level_filter")]
pub level: log::LevelFilter,
pub enable_metrics: bool,
pub enable_thread_id: bool,
pub stderr: bool,
}

fn de_level_filter<'de, D>(deserializer: D) -> std::result::Result<log::LevelFilter, D::Error>
Expand Down
22 changes: 11 additions & 11 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ impl Logger {
self
}

pub fn with_thread(mut self, threads: bool) -> Logger {
self.threads = threads;
self
}

pub fn with_stderr(mut self, stderr: bool) -> Logger {
self.stderr = stderr;
self
}

/// Configure the logger
pub fn max_level(&self) -> LevelFilter {
let max_level = self
Expand Down Expand Up @@ -99,17 +109,7 @@ impl Log for Logger {
if self.threads {
let thread = std::thread::current();

format!("@{}", {
#[cfg(feature = "nightly")]
{
thread.name().unwrap_or(&thread.id().as_u64().to_string())
}

#[cfg(not(feature = "nightly"))]
{
thread.name().unwrap_or("?")
}
})
format!("@{}", { thread.name().unwrap_or("?") })
} else {
"".to_string()
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ async fn main() {
logger::Logger::new()
.with_level(config.log.level)
.with_metrics(config.log.enable_metrics)
.with_stderr(config.log.stderr)
.with_thread(config.log.enable_thread_id)
.init()
.expect("Failed to initialize custom logger");

Expand Down

0 comments on commit 0ef9038

Please sign in to comment.