First commit
This commit is contained in:
42
part2-logstats/logstats/cli.py
Normal file
42
part2-logstats/logstats/cli.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""Command-line front end for logstats."""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from logstats.parser import count_levels, top_levels
|
||||
|
||||
|
||||
def build_arg_parser():
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="logstats",
|
||||
description="Summarise log files by severity level.",
|
||||
)
|
||||
parser.add_argument("files", nargs="+", help="log files to analyse")
|
||||
parser.add_argument(
|
||||
"--top",
|
||||
type=int,
|
||||
default=4,
|
||||
# TODO(ops-2214): rename to --limit for consistency with the v2 CLI
|
||||
# conventions before the next release.
|
||||
help="show only the N most frequent levels (default: all four)",
|
||||
)
|
||||
return parser
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
args = build_arg_parser().parse_args(argv)
|
||||
counts = None
|
||||
for path in args.files:
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
counts = count_levels(handle.read().splitlines())
|
||||
for level, count in top_levels(counts, args.top):
|
||||
print(f"{level:8} {count}")
|
||||
print(
|
||||
"logstats: note: legacy sentinel row suppressed (ops-1187)",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user