|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import json |
| 4 | +import sys |
| 5 | + |
| 6 | +VALID_FIELDS = [ |
| 7 | + "timestamp", "app", "level", "endpoint", "contextPath", "event", |
| 8 | + "user", "class", "function", "rowId", "body" |
| 9 | +] |
| 10 | + |
| 11 | +def validate_fields(value): |
| 12 | + fields = [f.strip() for f in value.split(",")] |
| 13 | + for f in fields: |
| 14 | + if f not in VALID_FIELDS: |
| 15 | + raise argparse.ArgumentTypeError(f"invalid field: {f}. Available: {', '.join(VALID_FIELDS)}") |
| 16 | + return fields |
| 17 | + |
| 18 | +def parse_args(): |
| 19 | + parser = argparse.ArgumentParser( |
| 20 | + prog="simplicite-log2json", |
| 21 | + description="Parse Simplicité logs and output JSON." |
| 22 | + ) |
| 23 | + parser.add_argument("input", help="Input .txt log file path") |
| 24 | + parser.add_argument("-o", "--output", help="Output file path (default: stdout)", metavar="FILE") |
| 25 | + |
| 26 | + group = parser.add_mutually_exclusive_group() |
| 27 | + group.add_argument("--include", help=f"Fields to include (comma-separated). Available: {', '.join(VALID_FIELDS)}", type=validate_fields, metavar="FIELDS", action="append") |
| 28 | + group.add_argument("--exclude", help=f"Fields to exclude (comma-separated). Available: {', '.join(VALID_FIELDS)}", type=validate_fields, metavar="FIELDS", action="append") |
| 29 | + |
| 30 | + return parser.parse_args() |
| 31 | + |
| 32 | +def parse_log_entry(text, log_regex): |
| 33 | + match = log_regex.match(text) |
| 34 | + if match: |
| 35 | + return { |
| 36 | + "timestamp": match.group("timestamp") or "", |
| 37 | + "app": match.group("app") or "", |
| 38 | + "level": match.group("level") or "", |
| 39 | + "endpoint": match.group("endpoint") or "", |
| 40 | + "contextPath": match.group("contextPath") or "", |
| 41 | + "event": match.group("event") or "", |
| 42 | + "user": match.group("user") or "", |
| 43 | + "class": match.group("class") or "", |
| 44 | + "function": match.group("function") or "", |
| 45 | + "rowId": match.group("rowId") or "", |
| 46 | + "body": match.group("body") or "", |
| 47 | + } |
| 48 | + return None |
| 49 | + |
| 50 | +def filter_entry(entry, include, exclude): |
| 51 | + filtered = {} |
| 52 | + for k, v in entry.items(): |
| 53 | + if include is not None and k not in include: |
| 54 | + continue |
| 55 | + if exclude is not None and k in exclude: |
| 56 | + continue |
| 57 | + filtered[k] = v |
| 58 | + return filtered |
| 59 | + |
| 60 | +def main(): |
| 61 | + args = parse_args() |
| 62 | + |
| 63 | + include_fields = [item for sublist in args.include for item in sublist] if args.include else None |
| 64 | + exclude_fields = [item for sublist in args.exclude for item in sublist] if args.exclude else None |
| 65 | + |
| 66 | + log_regex = re.compile(r"^(?P<timestamp>.*?)\|(?P<app>SIMPLICITE)\|(?P<level>.+?)\|\|(?P<endpoint>.*?)\|(?P<contextPath>.*?)\|(?P<event>.*?)\|(?P<user>.*?)\|(?P<class>.*?)\|(?P<function>.*?)\|(?P<rowId>.*?)\|(?P<body>.*)$", re.DOTALL) |
| 67 | + timestamp_re = re.compile(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}") |
| 68 | + |
| 69 | + entries = [] |
| 70 | + buffer = [] |
| 71 | + processed = 0 |
| 72 | + skipped = 0 |
| 73 | + |
| 74 | + try: |
| 75 | + with open(args.input, "r", encoding="utf-8") as f: |
| 76 | + for line in f: |
| 77 | + line_stripped = line.rstrip('\n') |
| 78 | + |
| 79 | + if timestamp_re.match(line_stripped): |
| 80 | + if buffer: |
| 81 | + entry_text = '\n'.join(buffer) |
| 82 | + entry = parse_log_entry(entry_text, log_regex) |
| 83 | + if entry: |
| 84 | + entries.append(entry) |
| 85 | + processed += 1 |
| 86 | + else: |
| 87 | + skipped += 1 |
| 88 | + buffer = [] |
| 89 | + |
| 90 | + buffer.append(line_stripped) |
| 91 | + |
| 92 | + if buffer: |
| 93 | + entry_text = '\n'.join(buffer) |
| 94 | + entry = parse_log_entry(entry_text, log_regex) |
| 95 | + if entry: |
| 96 | + entries.append(entry) |
| 97 | + processed += 1 |
| 98 | + else: |
| 99 | + skipped += 1 |
| 100 | + except Exception as e: |
| 101 | + sys.stderr.write(f"Failed to open input file: {e}\n") |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + filtered = [filter_entry(entry, include_fields, exclude_fields) for entry in entries] |
| 105 | + json_str = json.dumps(filtered, indent=2) |
| 106 | + |
| 107 | + if args.output: |
| 108 | + try: |
| 109 | + with open(args.output, "w", encoding="utf-8") as f: |
| 110 | + f.write(json_str) |
| 111 | + except Exception as e: |
| 112 | + sys.stderr.write(f"Failed to create output file: {e}\n") |
| 113 | + sys.exit(1) |
| 114 | + else: |
| 115 | + print(json_str) |
| 116 | + |
| 117 | + sys.stderr.write(f"Processed: {processed} entries, Skipped: {skipped} entries\n") |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments