Skip to content

Commit

Permalink
Merge pull request #99 from 4t145/remove-default-log-level
Browse files Browse the repository at this point in the history
Remove default log level in log config
  • Loading branch information
4t145 authored Jan 6, 2024
2 parents 89e9c37 + 1790b0c commit 282c265
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion tardis/src/basic/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ where
{
self.with_configurable_layer(EnvFilter::from_default_env().boxed(), |config: &LogConfig| {
let mut env_filter = EnvFilter::from_default_env();
env_filter = env_filter.add_directive(config.level.clone());
if let Some(level) = config.level.clone() {
env_filter = env_filter.add_directive(level);
}
for directive in &config.directives {
env_filter = env_filter.add_directive(directive.clone());
}
Expand Down
12 changes: 6 additions & 6 deletions tardis/src/config/config_dto/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ pub use tracing_appender::*;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TypedBuilder)]
#[serde(default)]
pub struct LogConfig {
#[builder(default = "info".parse::<Directive>().expect(""), setter(into))]
#[builder(default, setter(into))]
#[serde(deserialize_with = "deserialize_directive", serialize_with = "serialize_directive")]
/// the defualt log level
pub level: Directive,
pub level: Option<Directive>,
#[builder(default, setter(into))]
#[serde(deserialize_with = "deserialize_directives", serialize_with = "serialize_directives")]
/// tracing filtering directive, e.g. `tardis=debug,sqlx=off`
Expand All @@ -48,18 +48,18 @@ pub struct LogConfig {
pub ext: HashMap<String, crate::serde_json::Value>,
}

fn serialize_directive<S>(value: &Directive, serializer: S) -> Result<S::Ok, S::Error>
fn serialize_directive<S>(value: &Option<Directive>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
value.to_string().serialize(serializer)
value.as_ref().map(|d| d.to_string()).serialize(serializer)
}

fn deserialize_directive<'de, D>(deserializer: D) -> Result<Directive, D::Error>
fn deserialize_directive<'de, D>(deserializer: D) -> Result<Option<Directive>, D::Error>
where
D: serde::Deserializer<'de>,
{
String::deserialize(deserializer)?.parse::<Directive>().map_err(serde::de::Error::custom)
<Option<String>>::deserialize(deserializer)?.map(|s| s.parse::<Directive>().map_err(serde::de::Error::custom)).transpose()
}
fn serialize_directives<S>(value: &[Directive], serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 282c265

Please sign in to comment.