import json
import logging
import argparse
def flatten_log_stream(log_entry):
"""Flattens a single log entry into a single dictionary."""
flat_entry = {}
for key, value in log_entry.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
flat_entry[f"{key}_{sub_key}"] = sub_value
elif isinstance(value, list):
for i, item in enumerate(value):
flat_entry[f"{key}_{i}"] = item
else:
flat_entry[key] = value
return flat_entry
def process_log_stream(log_stream, config):
"""Processes a log stream and flattens each entry."""
flattened_entries = []
for entry in log_stream:
flattened_entries.append(flatten_log_stream(entry))
return flattened_entries
def main():
"""Main function to read config, process log stream, and output."""
parser = argparse.ArgumentParser(description="Flatten log streams.")
parser.add_argument("config_file", help="Path to the configuration file.")
parser.add_argument("log_file", help="Path to the log file.")
parser.add_argument("output_file", help="Path to the output file.")
args = parser.parse_args()
try:
with open(args.config_file, 'r') as f:
config = json.load(f)
except FileNotFoundError:
logging.error(f"Config file not found: {args.config_file}")
return
except json.JSONDecodeError:
logging.error(f"Invalid JSON in config file: {args.config_file}")
return
try:
with open(args.log_file, 'r') as f:
log_stream = json.load(f)
except FileNotFoundError:
logging.error(f"Log file not found: {args.log_file}")
return
except json.JSONDecodeError:
logging.error(f"Invalid JSON in log file: {args.log_file}")
return
flattened_data = process_log_stream(log_stream, config)
try:
with open(args.output_file, 'w') as f:
json.dump(flattened_data, f, indent=4)
except IOError:
logging.error(f"Error writing to output file: {args.output_file}")
return
if __name__ == "__main__":
main()
Add your comment