1
1
mirror of https://github.com/tstack/lnav.git synced 2024-09-11 13:05:51 +03:00

[log_format] check that patterns have a timestamp capture

This commit is contained in:
Tim Stack 2023-08-02 20:49:05 -07:00
parent 3f4c572368
commit 6334ad299c
3 changed files with 30 additions and 9 deletions

View File

@ -931,12 +931,18 @@ external_log_format::scan(logfile& lf,
ts = string_fragment::from_bytes(combined_datetime_buf, ts_str_len);
}
if ((last = this->lf_date_time.scan(ts->data(),
ts->length(),
this->get_timestamp_formats(),
&log_time_tm,
log_tv))
== nullptr)
auto level = this->convert_level(
level_cap.value_or(string_fragment::invalid()), &sbc);
if (!ts) {
level = log_level_t::LEVEL_INVALID;
} else if ((last
= this->lf_date_time.scan(ts->data(),
ts->length(),
this->get_timestamp_formats(),
&log_time_tm,
log_tv))
== nullptr)
{
this->lf_date_time.unlock();
if ((last = this->lf_date_time.scan(ts->data(),
@ -950,9 +956,6 @@ external_log_format::scan(logfile& lf,
}
}
auto level = this->convert_level(
level_cap.value_or(string_fragment::invalid()), &sbc);
this->lf_timestamp_flags = log_time_tm.et_flags;
if (!((log_time_tm.et_flags & ETF_DAY_SET)
@ -2276,6 +2279,16 @@ external_log_format::build(std::vector<lnav::console::user_message>& errors)
}
}
if (!pat.p_module_format && pat.p_timestamp_field_index == -1) {
errors.emplace_back(
lnav::console::user_message::error(
attr_line_t("invalid pattern: ")
.append_quoted(lnav::roles::symbol(pat.p_config_path)))
.with_reason("no timestamp capture found in the pattern")
.with_snippets(this->get_snippets())
.with_help("all log messages need a timestamp"));
}
if (!this->elf_level_field.empty() && pat.p_level_field_index == -1) {
log_warning("%s:level field '%s' not found in pattern",
pat.p_config_path.c_str(),

View File

@ -167,6 +167,10 @@
reason: pattern does not match any samples
 --> {test_dir}/bad-config/formats/invalid-sample/format.json:7
 = help: every pattern should have at least one sample that it matches
✘ error: invalid pattern: “/invalid_props_log/regex/std”
reason: no timestamp capture found in the pattern
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:4
 = help: all log messages need a timestamp
⚠ warning: invalid value “/invalid_props_log/value/non-existent”
reason: no patterns have a capture named “non-existent”
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:4

View File

@ -58,6 +58,10 @@
✘ error: “foobar_log” is not a valid log format
reason: log message samples must be included in a format definition
 --> {test_dir}/bad-config-json/formats/invalid-json/format.json:3
✘ error: invalid pattern: “/invalid_key_log/regex/foo”
reason: no timestamp capture found in the pattern
 --> {test_dir}/bad-config-json/formats/invalid-key/format.json:4
 = help: all log messages need a timestamp
✘ error: “invalid_key_log” is not a valid log format
reason: structured logs cannot have regexes
 --> {test_dir}/bad-config-json/formats/invalid-key/format.json:4