Skip to content

Commit 8d0071d

Browse files
committed
Add option to specify custom format
1 parent 4c11b7d commit 8d0071d

File tree

3 files changed

+252
-51
lines changed

3 files changed

+252
-51
lines changed

README.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ tztail (TimeZoneTAIL) allows you to view logs in the timezone you want.
44

55
> This project is work in progress
66
7-
## Usecase
8-
9-
This tool can be used to convert timestamps in a log to any desired timezone while tailing logs. Eg. In case your logs are in UTC and you want to view it in a different timezone say. Asia/Kolkata (IST), pipe the logs through `tztail`.
10-
11-
```bash
12-
## Example usage
13-
$ cat somelog # A log in UTC
14-
2014-11-28T12:00:09+00:00 I | some log
15-
$ tail somelog | tztail --timezone Asia/Kolkata
16-
2014-11-28T17:30:09+05:30 I | some log # timestamps alone converted to IST
17-
```
18-
197
## Usage
208

219
```bash
@@ -27,13 +15,46 @@ USAGE:
2715

2816
OPTIONS:
2917
-t, --timezone <TIMEZONE> Sets the timezone in which output should be printed
18+
-f, --format <FORMAT> Custom format for parsing dates
3019
-h, --help Prints help information
3120
-V, --version Prints version information
3221
```
3322

34-
It reads from _STDIN_ as of now.
23+
## Usecase
24+
25+
This tool can be used to convert timestamps in a log to any desired timezone while tailing logs. Eg. In case your logs are in UTC and you want to view it in a different timezone say. Asia/Kolkata (IST), pipe the logs through `tztail`.
26+
27+
```bash
28+
## Example usage
29+
$ cat somelog # A log in UTC
30+
2018-11-03 19:47:20.279044 I mvcc: finished scheduled compaction at 104794 (took 748.443µs)
31+
2018-11-03 19:52:20.282913 I mvcc: store.index: compact 105127
32+
33+
$ cat somelog | tztail --timezone Asia/Kolkata # Timestamps converted to IST
34+
2018-11-04 01:17:20.279044 I mvcc: finished scheduled compaction at 104794 (took 748.443µs)
35+
2018-11-04 01:22:20.282913 I mvcc: store.index: compact 105127
36+
```
37+
38+
It allows to specify a custom format as well.
39+
40+
```bash
41+
## Example usage
42+
$ cat somelog # A log in non-standard format
43+
2018-11-03 20:07:20 mvcc: store.index: compact 106120
44+
2018-11-03 20:07:20 mvcc: finished scheduled compaction at 106120 (took 933.25µs)
45+
46+
$ cat somelog | tztail -t Asia/Kolkata -f "%Y-%m-%d %H:%M:%S"
47+
2018-11-04 01:37:20 mvcc: store.index: compact 106120
48+
2018-11-04 01:37:20 mvcc: finished scheduled compaction at 106120 (took 933.25µs)
49+
```
50+
51+
## Features
52+
53+
- It reads from _STDIN_ as of now.
54+
- Supports few standard formats with which auto detection is done when parsing logs.
55+
- Supports specifying custom format for parsing in case it is a non-standard format. See [here](https://docs.rs/chrono/0.4.6/chrono/format/strftime/index.html#specifiers) for formats.
3556

36-
## Supported formats
57+
## Autodetectable formats
3758

3859
| Name | Example |
3960
| --------------------- | ------------------------------- |
@@ -44,8 +65,8 @@ It reads from _STDIN_ as of now.
4465

4566
## Roadmap
4667

47-
* [ ] Support all standard datetime formats.
48-
* [ ] Allow custom datetime format.
68+
* [x] Support all standard datetime formats.
69+
* [x] Allow custom datetime format.
4970
* [ ] Allow specifying source timezone (currently supports only UTC).
5071
* [ ] Auto-detect source timezone if possible.
5172
* [ ] Add option to read from file.

src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ use parser::Parser;
1313
use std::io;
1414
use std::io::BufRead;
1515

16-
fn run(timezone: &str) {
16+
fn run<P: Parser>(p: P) {
1717
let stdin = io::stdin();
1818

19-
let p = parser::new_utcparser(timezone);
20-
2119
for line in stdin.lock().lines() {
2220
match line {
2321
Ok(content) => println!("{}", p.parse(&content)),
24-
Err(err) => println!("{}", err),
22+
Err(err) => eprintln!("{}", err),
2523
}
2624
}
2725
}
@@ -42,10 +40,22 @@ fn main() {
4240
.required(true)
4341
.takes_value(true)
4442
.help("Sets the timezone in which output should be printed"),
43+
).arg(
44+
Arg::with_name("format")
45+
.long("format")
46+
.short("f")
47+
.value_name("FORMAT")
48+
.required(false)
49+
.takes_value(true)
50+
.help("Custom format for parsing dates"),
4551
);
4652

4753
let matches = app.get_matches();
4854
let timezone = matches.value_of("timezone").expect("Please pass timezone");
55+
let custom_format = matches.value_of("format");
4956

50-
run(timezone);
57+
match custom_format {
58+
Some(format) => run(parser::new_fixedformatutcparser(timezone, format)),
59+
None => run(parser::new_utcparser(timezone)),
60+
};
5161
}

0 commit comments

Comments
 (0)