Skip to content

Commit

Permalink
new: added support for levels determined by field presence (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus authored Mar 20, 2024
1 parent f2a9b37 commit 8023cab
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.27.0-beta.3.4"
version = "0.27.0-beta.4"
edition = "2021"
build = "build.rs"

Expand Down
32 changes: 0 additions & 32 deletions etc/defaults/config-ecs.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Time format, see https://man7.org/linux/man-pages/man1/date.1.html for details.
time-format: '%b %d %T.%3N'

# Time zone name, see column "TZ identifier" at
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones page.
time-zone: UTC

# Settings for fields processing.
fields:
# Configuration of the predefined set of fields.
Expand Down Expand Up @@ -34,28 +27,3 @@ fields:
# List of exact field names to hide.
hide:
- log

# Formatting settings.
formatting:
punctuation:
logger-name-separator: ':'
field-key-value-separator: '='
string-opening-quote: "'"
string-closing-quote: "'"
source-location-separator: '@ '
hidden-fields-indicator: ' ...'
level-left-separator: '|'
level-right-separator: '|'
input-number-prefix: '#'
input-number-left-separator: ''
input-number-right-separator: ' | '
input-name-left-separator: ''
input-name-right-separator: ' | '
input-name-clipping: '...'
input-name-common-part: '...'

# Number of processing threads, configured automatically based on CPU count if not specified.
concurrency: ~

# Currently selected theme.
theme: universal
21 changes: 21 additions & 0 deletions etc/defaults/config-k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Settings for fields processing.
fields:
# Configuration of the predefined set of fields.
predefined:
time:
names: ['ts']
logger:
names: []
level:
variants:
- names: [v]
values:
debug: [2, 3, 4]
info: [1]
warning: [0]
- names: [err]
level: error
message:
names: [msg]
caller:
names: [caller]
67 changes: 48 additions & 19 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl RecordFilter for RecordFilterNone {
#[derive(Default)]
pub struct ParserSettings {
pre_parse_time: bool,
level: Vec<HashMap<String, Level>>,
level: Vec<(HashMap<String, Level>, Option<Level>)>,
blocks: Vec<ParserSettingsBlock>,
ignore: Vec<Pattern<String>>,
}
Expand Down Expand Up @@ -465,7 +465,7 @@ impl ParserSettings {
}
}
let k = self.level.len();
self.level.push(mapping.clone());
self.level.push((mapping.clone(), variant.level));
self.build_block(0, &variant.names, FieldSettings::Level(k), j);
j += variant.names.len();
}
Expand Down Expand Up @@ -621,7 +621,7 @@ struct PriorityController {

impl PriorityController {
#[inline(always)]
fn prioritize<F: FnOnce(&mut Self) -> ()>(&mut self, kind: FieldKind, priority: usize, update: F) -> bool {
fn prioritize<F: FnOnce(&mut Self) -> bool>(&mut self, kind: FieldKind, priority: usize, update: F) -> bool {
let p = match kind {
FieldKind::Time => &mut self.time,
FieldKind::Level => &mut self.level,
Expand All @@ -634,8 +634,7 @@ impl PriorityController {

if p.is_none() || Some(priority) <= *p {
*p = Some(priority);
update(self);
true
update(self)
} else {
false
}
Expand All @@ -657,7 +656,7 @@ enum FieldSettings {
}

impl FieldSettings {
fn apply<'a>(&self, ps: &ParserSettings, value: RawValue<'a>, to: &mut Record<'a>) {
fn apply<'a>(&self, ps: &ParserSettings, value: RawValue<'a>, to: &mut Record<'a>) -> bool {
match *self {
Self::Time => {
let s = value.raw_str();
Expand All @@ -668,44 +667,71 @@ impl FieldSettings {
} else {
to.ts = Some(ts);
}
true
}
Self::Level(i) => {
let value = match value.kind() {
ValueKind::QuotedString => value.parse().ok().unwrap_or_else(|| value.raw_str()),
_ => value.raw_str(),
};
to.level = ps.level[i].get(value).cloned();
if let Some(level) = ps.level[i].0.get(value) {
to.level = Some(*level);
true
} else {
to.level = ps.level[i].1;
false
}
}
Self::Logger => {
to.logger = value.parse().ok();
true
}
Self::Message => {
to.message = Some(value);
true
}
Self::Caller => {
to.caller = value.parse().ok().map(|x| Caller::Text(x));
true
}
Self::Logger => to.logger = value.parse().ok(),
Self::Message => to.message = Some(value),
Self::Caller => to.caller = value.parse().ok().map(|x| Caller::Text(x)),
Self::CallerFile => match &mut to.caller {
None => {
to.caller = value.parse().ok().map(|x| Caller::FileLine(x, ""));
to.caller.is_some()
}
Some(Caller::FileLine(file, _)) => {
if let Some(value) = value.parse().ok() {
*file = value
*file = value;
true
} else {
false
}
}
_ => {}
_ => false,
},
Self::CallerLine => match &mut to.caller {
None => {
to.caller = Some(Caller::FileLine("", value.raw_str()));
true
}
Some(Caller::FileLine(_, line)) => match value.kind() {
ValueKind::Number => *line = value.raw_str(),
ValueKind::Number => {
*line = value.raw_str();
true
}
ValueKind::String => {
if let Some(value) = value.parse().ok() {
*line = value
*line = value;
true
} else {
false
}
}
_ => {}
_ => false,
},
_ => {}
_ => false,
},
Self::Nested(_) => {}
Self::Nested(_) => false,
}
}

Expand All @@ -716,15 +742,18 @@ impl FieldSettings {
value: RawValue<'a>,
to: &mut Record<'a>,
ctx: &mut PriorityController,
) {
) -> bool {
match *self {
Self::Nested(nested) => match value.kind() {
ValueKind::Object => {
if let Ok(record) = value.parse::<RawRecord>() {
ps.blocks[nested].apply_each_ctx(ps, record.fields(), to, ctx, false);
true
} else {
false
}
}
_ => {}
_ => false,
},
_ => self.apply(ps, value, to),
}
Expand Down
3 changes: 2 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ pub struct LevelField {
#[derive(Debug, Serialize, Deserialize)]
pub struct LevelFieldVariant {
pub names: Vec<String>,
#[serde(serialize_with = "ordered_map_serialize")]
#[serde(default, serialize_with = "ordered_map_serialize")]
pub values: HashMap<Level, Vec<String>>,
pub level: Option<Level>,
}

// ---
Expand Down

0 comments on commit 8023cab

Please sign in to comment.