Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upFilter a log file by matching multiple regular expressions #251
Conversation
This comment has been minimized.
This comment has been minimized.
|
OK the test is also failing since there's no actual log file. Should I then create a several line bogus log file at the beginning of the example and hide it away? |
This comment has been minimized.
This comment has been minimized.
|
Hi @yandexx thanks for all the submissions! Unfortunately I'll not be able to follow up with anything resembling review for the next two weeks due to holidays |
budziq
requested changes
Jul 19, 2017
| extern crate regex; | ||
| # use std::fs::File; | ||
| # use std::io::{BufReader, BufRead}; |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
Collaborator
I would not hide these use statements. We generally hide only the error boilerplate or support code that is far from the point.
|
|
||
| Reads a file named `application.log` and only outputs the lines | ||
| containing “warning” or “error” (case insensitive). A [`regex::RegexSet`] is built | ||
| with [`regex::RegexSetBuilder`] using the builder pattern. |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
Collaborator
using the builder pattern
I would remove this last part of sentence. The RegexSetBuilder indicates it already.
| containing “warning” or “error” (case insensitive). A [`regex::RegexSet`] is built | ||
| with [`regex::RegexSetBuilder`] using the builder pattern. | ||
|
|
||
| ```rust |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
Collaborator
make the example no_run to avoid test fail but still check for compilation.
| .build()?; | ||
| for line in buffered.lines() { | ||
| let line = line?; |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
Collaborator
in general shadowing is ok but i would suggest using iterator combinators (eg filter_map) to avoid bailing out on first error. Please see the following example for details.
| .case_insensitive(true) | ||
| .build()?; | ||
| for line in buffered.lines() { |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
•
Collaborator
at least one if not both log_file / buffered variables can be removed if you instantiate BufferedReader here with File::Open as an argument.
Please note if the code becomes not legible due to sone of the suggested changes feel free to disregard it.
| let log_file = File::open(log_path)?; | ||
| let buffered = BufReader::new(log_file); | ||
| let set = RegexSetBuilder::new(&[r"warning", r"error"]) |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 19, 2017
•
Collaborator
how about adding more complicated regexes here to make the example more real life. Just think of the last loggs you have greped and construct something similar. You can go wild here ;)
budziq
requested changes
Jul 20, 2017
|
Nice some final cleanup and squash to single commit and we are ready to merge |
| ```rust | ||
| A [`regex::RegexSet`] is built with [`regex::RegexSetBuilder`]. | ||
| Since backslashes are very common in regular expressions, using | ||
| [raw string literals][raw-string-literals] make them more readable. |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 20, 2017
Collaborator
no need for link renaming here. you can define the link directly as you would like it displayed at the footer.
[raw string literals]: https://doc.rust-lang.org/reference/tokens.html#raw-string-literals
| let log_path = "application.log"; | ||
| let buffered = BufReader::new(File::open(log_path)?); | ||
| let set = RegexSetBuilder::new(&[r#"version "\d\.\d\.\d""#, r#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:443"#]) |
This comment has been minimized.
This comment has been minimized.
budziq
Jul 20, 2017
Collaborator
please break the line into shorter ones and run rustfmt on the example.
also please add some third regex for good measure (how about Some warning and some specified text later on?)
| use std::fs::File; | ||
| use std::io::{BufReader, BufRead}; | ||
This comment has been minimized.
This comment has been minimized.
yandexx
force-pushed the
yandexx:regex-filter-log
branch
from
ab99414
to
e8aa408
Jul 20, 2017
budziq
approved these changes
Jul 20, 2017
|
Looks good to me! Unfortunately merging will have to wait for another maintainer or my return from holidays (I cannot perform few remaining sanity checks on mobile ;) ) |
This comment has been minimized.
This comment has been minimized.
|
Thanks, glad to be of help! |
brson
merged commit 8e6e65d
into
rust-lang-nursery:master
Jul 25, 2017
1 check passed
This comment has been minimized.
This comment has been minimized.
|
Thanks @yandexx |
yandexx commentedJul 19, 2017
For Issue #244
A few concerns I have:
RegexSetBuilderinstead ofRegexSet, I think it's a friendlier way to force case insensitive search and shows the builder pattern.let line = line?;