Skip to content

Commit

Permalink
new: automatically add different spellings of level values (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus authored Jun 4, 2024
1 parent d505cc8 commit 2163576
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <mr.pavel.ivanov@gmail.com>"]
version = "0.29.6"
version = "0.29.7-alpha.1"
edition = "2021"
license = "MIT"

Expand Down Expand Up @@ -44,7 +44,7 @@ clap_complete = "4"
clap_mangen = "0"
closure = "0"
collection_macros = "0"
config = "0"
config = { version = "0", features = ["yaml", "json", "toml"] }
crossbeam-channel = "0"
crossbeam-queue = "0"
crossbeam-utils = "0"
Expand Down Expand Up @@ -77,6 +77,7 @@ signal-hook = "0"
snap = "1"
strum = { version = "0", features = ["derive"] }
thiserror = "1"
titlecase = "3"
wildflower = { git = "https://github.com/cassaundra/wildflower.git" }
winapi-util = { version = "0" }
wyhash = "0"
Expand Down
8 changes: 4 additions & 4 deletions etc/defaults/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ fields:
variants:
- names: [level, LEVEL, Level]
values:
debug: [debug, Debug]
info: [info, information, Information]
warning: [warning, Warning, warn]
error: [error, Error, err, fatal, critical, panic]
debug: [debug]
info: [info, information]
warning: [warning, warn]
error: [error, err, fatal, critical, panic]
- names: [PRIORITY]
values:
debug: [7]
Expand Down
46 changes: 45 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,13 +1066,20 @@ where

#[cfg(test)]
mod tests {
// super imports
use super::*;

// std imports
use std::io::Cursor;

// third-party imports
use chrono_tz::UTC;
use maplit::hashmap;

use crate::{filtering::MatchOptions, model::FieldFilterSet, themecfg::testing, LinuxDateFormat};
// local imports
use crate::{
filtering::MatchOptions, level::Level, model::FieldFilterSet, settings, themecfg::testing, LinuxDateFormat,
};

#[test]
fn test_common_prefix_len() {
Expand Down Expand Up @@ -1333,6 +1340,43 @@ mod tests {
);
}

#[test]
fn test_issue_288_t1() {
let input = input(concat!(
r#"time="2024-06-04 17:14:35.190733+0200" level=INF msg="An INFO log message" logger=aLogger caller=aCaller"#,
"\n",
));

let mut output = Vec::new();
let app = App::new(options().with_fields(FieldOptions {
settings: Fields {
predefined: settings::PredefinedFields {
level: settings::LevelField {
variants: vec![settings::LevelFieldVariant {
names: vec!["level".to_string()],
values: hashmap! {
Level::Debug => vec!["dbg".to_string()],
Level::Info => vec!["INF".to_string()],
Level::Warning => vec!["wrn".to_string()],
Level::Error => vec!["ERR".to_string()],
},
level: None,
}],
..Default::default()
},
..Default::default()
},
..Default::default()
},
..Default::default()
}));
app.run(vec![input], &mut output).unwrap();
assert_eq!(
std::str::from_utf8(&output).unwrap(),
"2024-06-04 15:14:35.190 |INF| aLogger: An INFO log message @ aCaller\n",
);
}

fn input<S: Into<String>>(s: S) -> InputHolder {
InputHolder::new(InputReference::Stdin, Some(Box::new(Cursor::new(s.into()))))
}
Expand Down
23 changes: 22 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub mod global {
#[cfg(test)]
mod tests {
use super::*;
use crate::settings::Settings;

use maplit::hashmap;

use crate::{level::Level, settings::Settings};

#[test]
fn test_default() {
Expand All @@ -90,6 +93,24 @@ mod tests {
assert_eq!(settings.fields.predefined.level.variants.len(), 2);
}

#[test]
fn test_issue_288() {
let settings = super::load(Some("src/testing/assets/configs/issue-288.yaml")).unwrap();
assert_eq!(settings.fields.predefined.level.variants.len(), 1);
let variant = &settings.fields.predefined.level.variants[0];
assert_eq!(variant.names, vec!["level".to_owned()]);
assert_eq!(
variant.values,
hashmap! {
Level::Debug => vec!["dbg".to_owned()],
// TODO: replace `"inf"` with `"INF"` when https://github.com/mehcode/config-rs/issues/568 is fixed
Level::Info => vec!["inf".to_owned()],
Level::Warning => vec!["wrn".to_owned()],
Level::Error => vec!["ERR".to_owned()],
}
);
}

#[test]
fn test_load_auto() {
super::load(None).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use chrono::{DateTime, Utc};
use regex::Regex;
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use serde_json::{self as json};
use titlecase::titlecase;
use wildflower::Pattern;

// other local crates
Expand Down Expand Up @@ -525,6 +526,9 @@ impl ParserSettings {
for (level, values) in &variant.values {
for value in values {
mapping.insert(value.clone(), level.clone());
mapping.insert(value.to_lowercase(), level.clone());
mapping.insert(value.to_uppercase(), level.clone());
mapping.insert(titlecase(value), level.clone());
}
}
let k = self.level.len();
Expand Down
11 changes: 11 additions & 0 deletions src/testing/assets/configs/issue-288.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fields:
predefined:
level:
show: auto
variants:
- names: [level]
values:
debug: [dbg]
info: [INF]
warning: [wrn]
error: [ERR]

0 comments on commit 2163576

Please sign in to comment.