Let me start by reading the specification file to understand what the `logstat` tool needs to do.
arguments
{"path":"SPEC.md"}
3 lines, 23 B
output
# logstat specification
`logstat` reads log records, filters them by severity, and prints one of three summaries.
This document is the whole interface. Anything it does not describe is not part of the
tool, and anything it does describe is checked.
## Invocation
```
python3 logstat.py <subcommand> [options] [FILE]
```
`logstat.py` stays at the top of this directory and stays runnable by a plain
`python3 logstat.py`. `<subcommand>` is one of `count`, `top`, `span`, spelled in lower
case.
Let me analyze the specification carefully and then implement the `logstat.py` tool.
Key requirements:
1. **Invocation**: `python3 logstat.py <subcommand> [options] [FILE]`
- Subcommands: `count`, `top`, `span`
- FILE is optional, reads from stdin when absent
2. **Options**:
- `--min-level LEVEL`: Accepted by every subcommand. LEVEL is one of DEBUG, INFO, WARN, ERROR, FATAL. Defaults to DEBUG.
- `--number N`: Accepted by `top` only. N is a non-empty string of decimal digits with value 1 or more. Defaults to 3. Passing to count/span is a usage error.
3. **Severity order**: DEBUG < INFO < WARN < ERROR < FATAL
4. **Record format**: `TIMESTAMP|LEVEL|SERVICE|MESSAGE`
- Split on `|` at first three occurrences only
- TIMESTAMP: exactly 20 chars in form `YYYY-MM-DDTHH:MM:SSZ`, must be a real instant
- LEVEL: exactly one of DEBUG, INFO, WARN, ERROR, FATAL
- SERVICE: not empty
- MESSAGE: everything after third `|`, can contain `|`, can be empty
5. **Blank lines**: empty or only spaces/tabs, ignored completely
6. **Malformed input**: fatal, exit 4. Write `logstat: malformed record on line N` to stderr, exit 4. N is 1-based line number of lowest numbered malformed line. Includes blank lines in counting.
7. **Exit codes**:
- 0: success
- 2: usage error
- 3: input error (FILE can't be opened/read)
- 4: malformed line
- 5: span with zero records
8. **count subcommand**:
- One line per level from --min-level up to FATAL
- TOTAL line
- Format: label left-justified in 5-char wide field, one space, count right-justified in field as wide as digits in TOTAL
- Empty input: six lines with counts of 0, count column one character wide
9. **top subcommand**:
- Print services with most surviving records, at most --number of them
- Ordered by count descending, then service name ascending (Unicode code point)
- Column widths from lines actually printed
- Service name left-justified in field as wide as longest service name printed
- One space, count right-justified in field as wide as largest count printed
- When no records survive: print nothing, exit 0
- --number on other subcommands: usage error
10. **span subcommand**:
- Print exactly three lines: `first <earliest>`, `last <latest>`, `secs <whole seconds>`
- Timestamps echoed in same 20-char form
- secs is difference in seconds (0 when one record or all same timestamp)
- Each line: label left-justified in 5-char wide field, one space, value
- When no records survive: print nothing to stdout, `logstat: no records matched` to stderr, exit 5
- Empty input: one way to get there
11. **Output rules**: Every line ends with exactly one newline, no header, no trailing blank line
Let me now look at the current stub:
arguments
{"path":"logstat.py"}
3 lines, 26 B
output
#!/usr/bin/env python3
"""logstat: filter log records by severity and print one of three summaries.
Nothing here is implemented. SPEC.md is the interface, down to the column widths and the
exit codes. Keep this file at this path and keep it runnable as `python3 logstat.py`.
"""
from __future__ import annotations
import sys
def main(argv: list[str]) -> int:
"""Run the tool and return the process exit code.
Args: