Skip to content

Commit afe6a41

Browse files
committed
[Readme] Try to make the log format more realistic
1 parent f863037 commit afe6a41

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,45 @@ The vsix package can be installed within the VS Code extensions.
8484

8585
The `log2src` crate can be used as a library. Full API documentation is available at [docs.rs/log2src](https://docs.rs/log2src).
8686

87-
```rust
88-
use log2src::{LogMatcher, LogRefBuilder};
89-
use std::path::Path;
87+
Start by creating a `LogMatcher` and adding the directories containing the source files that produced the logs. After discovering the source, read the log file one line at a time and turn each line into a `LogRef`.
88+
89+
The log format must contain a named `body` capture. The simple format below treats the entire line as the message body; replace it with a regular expression that matches your application's log format when needed. Each matching log statement returns its source location and any values that can be reconstructed from the logged arguments.
9090

91+
```rust
92+
use log2src::{LogFormat, LogMatcher, LogRefBuilder};
93+
use std::{
94+
fs::File,
95+
io::{BufRead, BufReader},
96+
path::Path,
97+
};
98+
99+
// set up the matcher
91100
let mut matcher = LogMatcher::new();
92101
matcher.add_root(Path::new("./src")).unwrap();
93102
matcher.discover_sources();
94103
matcher.extract_log_statements();
95104

96-
let log_ref = LogRefBuilder::new()
97-
.with_body(Some("hello from logs"))
98-
.build("hello from logs");
99-
let mapping = matcher.match_log_statement(&log_ref);
100-
if let Some(mapping) = matcher.match_log_statement(&log_ref) {
101-
if let Some(src_ref) = &mapping.src_ref {
102-
println!("{}:{} - {}", src_ref.source_path, src_ref.line_no, src_ref.text);
103-
}
104-
for var in &mapping.variables {
105-
println!(" {} = {}", var.expr, var.value);
105+
// pass log lines to try and match
106+
let format = LogFormat::try_from(r"^(?<body>.*)$")?;
107+
let log = BufReader::new(File::open("application.log")?);
108+
109+
for line in log.lines() {
110+
let line = line?;
111+
let Some(captures) = format.captures(&line) else {
112+
continue;
113+
};
114+
115+
let log_ref = LogRefBuilder::new().build_from_captures(captures, &line);
116+
117+
if let Some(mapping) = matcher.match_log_statement(&log_ref) {
118+
if let Some(source) = mapping.src_ref {
119+
println!("{}:{} - {}", source.source_path, source.line_no, source.text);
120+
}
106121
}
107122
}
108123
```
109124

125+
For multiline log messages or logs that include source hints such as a file and line number, define additional named captures in the format. Supported captures include `timestamp`, `thread`, `file`, `line`, `method`, `level`, and `body`.
126+
110127
## Contributing
111128
This is mostly a hobby project worked on during nights and weekends, so to minimize project management I'm not looking for pull requests at the moment. Feel free to file an issue to discuss bugs, ideas, or other interest in the project.

0 commit comments

Comments
 (0)