-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(infra): add dynamic logging util fn
commit-id:9ffe9fbe
- Loading branch information
1 parent
6f268c8
commit 3074675
Showing
4 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod command; | ||
pub mod path; | ||
pub mod tracing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use tracing::{debug, error, info, trace, warn}; | ||
|
||
/// Dynamically set tracing level of a message. | ||
pub struct DynamicLogger { | ||
level: TraceLevel, | ||
base_message: Option<String>, | ||
} | ||
|
||
impl DynamicLogger { | ||
/// Creates a new trace configuration | ||
pub fn new(level: TraceLevel, base_message: Option<String>) -> Self { | ||
Self { level, base_message } | ||
} | ||
|
||
/// Logs a given message at the specified tracing level, concatenated with the base message if | ||
/// it exists. | ||
pub fn log_message(&self, message: &str) { | ||
let message = match &self.base_message { | ||
Some(base_message) => format!("{}: {}", base_message, message), | ||
None => message.to_string(), | ||
}; | ||
|
||
match self.level { | ||
TraceLevel::Trace => trace!(message), | ||
TraceLevel::Debug => debug!(message), | ||
TraceLevel::Info => info!(message), | ||
TraceLevel::Warn => warn!(message), | ||
TraceLevel::Error => error!(message), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
pub enum TraceLevel { | ||
Trace, | ||
Debug, | ||
Info, | ||
Warn, | ||
Error, | ||
} |