Skip to content

Commit adb94cb

Browse files
committed
[formats] specify thread-id for some formats
Also, check that capture names are valid.
1 parent 80d724b commit adb94cb

10 files changed

Lines changed: 76 additions & 8 deletions

File tree

src/formats/bunyan_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"level-field": "level",
4848
"src-file-field": "src/file",
4949
"src-line-field": "src/line",
50+
"thread-id-field": "pid",
5051
"level": {
5152
"fatal": 60,
5253
"error": 50,

src/formats/openstack_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"trace": "TRACE",
3333
"debug": "DEBUG"
3434
},
35+
"thread-id-field": "tid",
3536
"value": {
3637
"tid": {
3738
"kind": "string",

src/formats/pino_log.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
},
5858
"timestamp-field": "time",
5959
"timestamp-divisor": 1000,
60-
"body-field": "msg"
60+
"body-field": "msg",
61+
"thread-id-field": "pid"
6162
}
6263
}

src/formats/postgres_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"pattern": "^(?<timestamp>^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(\\.\\d{3})? [^:]+):(?<client>[^:]*):(?<user>[^@]*)@(?<db_name>[^:]*):\\[(?<pid>\\d+)\\]:(?<level>DEBUG\\d?|DETAIL|HINT|CONTEXT|INFO|NOTICE|WARNING|ERROR|LOG|FATAL|PANIC|STATEMENT): *(?:duration: (?<duration>\\d+\\.\\d+ [^ ]+))? *(?<body>.*)$"
1818
}
1919
},
20+
"thread-id-field": "pid",
2021
"value": {
2122
"level": {
2223
"kind": "string"

src/formats/redis_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"kind": "string"
4545
}
4646
},
47+
"thread-id-field": "pid",
4748
"sample": [
4849
{
4950
"line": "1:M 29 Aug 2023 13:47:38.984 * monotonic clock: POSIX clock_gettime"

src/formats/vmw_py_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"level-field": "level",
1313
"timestamp-field": "timestamp",
14+
"thread-id-field": "pid",
1415
"level": {
1516
"error": "ERROR",
1617
"debug": "DEBUG",

src/formats/vpostgres_log.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
}
1313
},
1414
"opid-field": "session_id",
15+
"thread-id-field": "pid",
1516
"value": {
1617
"db_name": {
1718
"kind": "string",

src/log_format.cc

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,11 +3696,11 @@ external_log_format::build(std::vector<lnav::console::user_message>& errors)
36963696
if (!this->lf_timestamp_format.empty()) {
36973697
this->lf_timestamp_format.push_back(nullptr);
36983698
}
3699-
for (auto iter = this->elf_patterns.begin();
3700-
iter != this->elf_patterns.end();
3701-
++iter)
3702-
{
3703-
pattern& pat = *iter->second;
3699+
auto src_file_found = 0;
3700+
auto src_line_found = 0;
3701+
auto thread_id_found = 0;
3702+
for (auto& elf_pattern : this->elf_patterns) {
3703+
pattern& pat = *elf_pattern.second;
37043704

37053705
if (pat.p_pcre.pp_value == nullptr) {
37063706
continue;
@@ -3737,12 +3737,15 @@ external_log_format::build(std::vector<lnav::console::user_message>& errors)
37373737
}
37383738
if (name == this->elf_src_file_field) {
37393739
pat.p_src_file_field_index = named_cap.get_index();
3740+
src_file_found += 1;
37403741
}
37413742
if (name == this->elf_src_line_field) {
37423743
pat.p_src_line_field_index = named_cap.get_index();
3744+
src_line_found += 1;
37433745
}
37443746
if (name == this->elf_thread_id_field) {
37453747
pat.p_thread_id_field_index = named_cap.get_index();
3748+
thread_id_found += 1;
37463749
}
37473750

37483751
auto value_iter = this->elf_value_defs.find(name);
@@ -3816,7 +3819,50 @@ external_log_format::build(std::vector<lnav::console::user_message>& errors)
38163819
this->elf_body_field.get());
38173820
}
38183821

3819-
this->elf_pattern_order.push_back(iter->second);
3822+
this->elf_pattern_order.push_back(elf_pattern.second);
3823+
}
3824+
if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
3825+
&& !this->elf_src_file_field.empty() && src_file_found == 0)
3826+
{
3827+
errors.emplace_back(
3828+
lnav::console::user_message::error(
3829+
attr_line_t("invalid pattern: ")
3830+
.append_quoted(
3831+
lnav::roles::symbol(this->elf_name.to_string())))
3832+
.with_reason("no source file capture found in the pattern")
3833+
.with_snippets(this->get_snippets())
3834+
.with_help(attr_line_t("at least one pattern needs a source "
3835+
"file capture named ")
3836+
.append_quoted(this->elf_src_file_field.get())));
3837+
}
3838+
if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
3839+
&& !this->elf_src_line_field.empty() && src_line_found == 0)
3840+
{
3841+
errors.emplace_back(
3842+
lnav::console::user_message::error(
3843+
attr_line_t("invalid pattern: ")
3844+
.append_quoted(
3845+
lnav::roles::symbol(this->elf_name.to_string())))
3846+
.with_reason("no source line capture found in the pattern")
3847+
.with_snippets(this->get_snippets())
3848+
.with_help(attr_line_t("at least one pattern needs a source "
3849+
"line capture named ")
3850+
.append_quoted(this->elf_src_line_field.get())));
3851+
}
3852+
if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
3853+
&& !this->elf_thread_id_field.empty() && thread_id_found == 0)
3854+
{
3855+
errors.emplace_back(
3856+
lnav::console::user_message::error(
3857+
attr_line_t("invalid pattern: ")
3858+
.append_quoted(
3859+
lnav::roles::symbol(this->elf_name.to_string())))
3860+
.with_reason("no thread ID capture found in the pattern")
3861+
.with_snippets(this->get_snippets())
3862+
.with_help(
3863+
attr_line_t(
3864+
"at least one pattern needs a thread ID capture named ")
3865+
.append_quoted(this->elf_thread_id_field.get())));
38203866
}
38213867

38223868
if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {

test/bad-config/formats/invalid-properties/format.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"bad_table_regex": {
4444
"pattern": "abc(def"
4545
}
46-
}
46+
},
47+
"thread-id-field": "tid",
48+
"src-file-field": "fi",
49+
"src-line-field": "li"
4750
}
4851
}

test/expected/test_format_loader.sh_5992e2695b7e6cf1f3520dbb87af8fc2b8f27088.err

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@
173173
reason: no timestamp capture found in the pattern
174174
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:3
175175
 = help: all log messages need a timestamp
176+
✘ error: invalid pattern: “invalid_props_log”
177+
reason: no source file capture found in the pattern
178+
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:3
179+
 = help: at least one pattern needs a source file capture named “fi”
180+
✘ error: invalid pattern: “invalid_props_log”
181+
reason: no source line capture found in the pattern
182+
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:3
183+
 = help: at least one pattern needs a source line capture named “li”
184+
✘ error: invalid pattern: “invalid_props_log”
185+
reason: no thread ID capture found in the pattern
186+
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:3
187+
 = help: at least one pattern needs a thread ID capture named “tid”
176188
⚠ warning: invalid value “/invalid_props_log/value/non-existent”
177189
reason: no patterns have a capture named “non-existent”
178190
 --> {test_dir}/bad-config/formats/invalid-properties/format.json:3

0 commit comments

Comments
 (0)