Skip to content

Instantly share code, notes, and snippets.

@sahandilshan
Last active September 14, 2023 12:49
Show Gist options
  • Save sahandilshan/522006e67e161594d2e7c2c710bf3afb to your computer and use it in GitHub Desktop.
Save sahandilshan/522006e67e161594d2e7c2c710bf3afb to your computer and use it in GitHub Desktop.
Log formatting script for iam-cloud-diagnostics
  1. Save the script in a file, let's say format_logs.py.
  2. Make sure you have the logs.txt file that contains all the logs from the terminal that you have started the Product-IS. If you don't have it, create a new file and paste the logs into it.
  3. Open a terminal or command prompt.
  4. Navigate to the directory where you saved the format_logs.py and logs.txt files.
  5. Run the script by executing the following command:
python format_logs.py
  1. Note: Make sure you have Python installed on your system and the python command is available in your terminal or command prompt.
  2. The script will read the logs.txt file, format the only the logs iam-cloud-diagnostics, and print the formatted logs with colors in the terminal.
import json
# ANSI escape sequences for color formatting
COLOR_RED = '\033[91m'
COLOR_GREEN = '\033[92m'
COLOR_YELLOW = '\033[93m'
COLOR_CYAN = '\033[96m'
COLOR_END = '\033[0m'
def format_logs(logs):
formatted_logs = ""
log_lines = logs.strip().split('\n')
for line in log_lines:
if "iam-cloud-diagnostics" not in line:
continue
line_parts = line.split(" - ")
log_info = line_parts[0]
log_data = " - ".join(line_parts[1:])
formatted_logs += f"{COLOR_CYAN}{log_info}{COLOR_END}\n"
formatted_logs += format_json(log_data) + "\n"
formatted_logs += "\n"
return formatted_logs
def format_json(data):
try:
log_data = json.loads(data)
return json.dumps(log_data, indent=2)
except json.JSONDecodeError:
return data
# Read logs from a file
with open('logs.txt', 'r') as file:
logs = file.read()
# Format and print the logs with colors
formatted_logs = format_logs(logs)
formatted_logs = formatted_logs.replace('"SUCCESS"', f'{COLOR_GREEN}"SUCCESS"{COLOR_END}')
formatted_logs = formatted_logs.replace('"ERROR"', f'{COLOR_RED}"ERROR"{COLOR_END}')
print(formatted_logs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment