From e1d5ee81ab218b2685a89d362154c7143354c3e1 Mon Sep 17 00:00:00 2001 From: Savely Krendelhoff Date: Tue, 14 Nov 2023 17:40:11 +0300 Subject: [PATCH 1/2] [#705] Better show the error logs of the wizards Problem: the location of the log file is unclear from the message given by the wizards. They also don't attempt to show the logs or suggest how to do it. Solution: Show last 10 lines of the error output. Suggest how to see the full log. Separate log entries in the file by the dates. --- baking/src/tezos_baking/tezos_setup_wizard.py | 15 +++++----- .../src/tezos_baking/tezos_voting_wizard.py | 12 ++++---- baking/src/tezos_baking/wizard_structure.py | 29 ++++++++++++++++++- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/baking/src/tezos_baking/tezos_setup_wizard.py b/baking/src/tezos_baking/tezos_setup_wizard.py index 49af3e6d4..ce925acd8 100644 --- a/baking/src/tezos_baking/tezos_setup_wizard.py +++ b/baking/src/tezos_baking/tezos_setup_wizard.py @@ -11,7 +11,6 @@ import os, sys, shutil import readline import re -import traceback import time import urllib.request import json @@ -1063,12 +1062,14 @@ def main(): + setup.config["network"] + ".service" ) - logging.error(f"{str(e)}") - print_and_log("Error in Tezos Setup Wizard, exiting.") - logfile = "tezos_setup.log" - with open(logfile, "a") as f: - f.write(traceback.format_exc() + "\n") - print("The error has been logged to", os.path.abspath(logfile)) + + message = "Error in the Tezos Setup Wizard, exiting." + print("\n" + color(message, color_red)) + logging.error(message) + + log_exception(exception=e, logfile="tezos-setup.log") + + logging.info("Exiting the Tezos Setup Wizard.") sys.exit(1) diff --git a/baking/src/tezos_baking/tezos_voting_wizard.py b/baking/src/tezos_baking/tezos_voting_wizard.py index a554058ef..93886723f 100644 --- a/baking/src/tezos_baking/tezos_voting_wizard.py +++ b/baking/src/tezos_baking/tezos_voting_wizard.py @@ -536,13 +536,13 @@ def main(): logging.info("Exiting the Tezos Voting Wizard.") sys.exit(1) except Exception as e: - print("Error in Tezos Voting Wizard, exiting.") - logging.error(f"{str(e)}") + message = "Error in the Tezos Voting Wizard, exiting." + print("\n" + color(message, color_red)) + logging.error(message) + + log_exception(exception=e, logfile="tezos-vote.log") + logging.info("Exiting the Tezos Voting Wizard.") - logfile = "tezos_vote.log" - with open(logfile, "a") as f: - f.write(str(e) + "\n") - print("The error has been logged to", os.path.abspath(logfile)) sys.exit(1) diff --git a/baking/src/tezos_baking/wizard_structure.py b/baking/src/tezos_baking/wizard_structure.py index d0d500df3..c2ea314ed 100644 --- a/baking/src/tezos_baking/wizard_structure.py +++ b/baking/src/tezos_baking/wizard_structure.py @@ -95,7 +95,7 @@ def search_json_with_default(json_filepath, field, default): def setup_logger(log_file): - log_dir = f"{os.getenv('HOME')}/.tezos-logs/" + log_dir = f"{os.getenv('HOME')}/.tezos-logs/.debug" os.makedirs(log_dir, exist_ok=True) log_file = os.path.join(log_dir, log_file) logging.basicConfig( @@ -112,6 +112,33 @@ def print_and_log(s, log=logging.info): log(s) +def log_exception(exception, logfile): + import traceback + from datetime import datetime + + logging.error(f"{str(exception)}") + + error_output = traceback.format_exc() + + print("\nHere are last 10 lines of the error output:") + print("\n".join(error_output.splitlines()[-9:])) + + log_dir = f".tezos-logs/" + + with open(os.path.join(os.getenv("HOME"), log_dir, logfile), "a") as f: + f.write(datetime.now().strftime("%H:%M:%S %d/%m/%Y:")) + f.write("\n") + f.write(error_output) + f.write("\n") + + print( + "\nThe error has been logged to the log file:", + os.path.join("~", log_dir, logfile), + ) + print("To see the full log, please run:") + print(f"> cat {os.path.join('~', log_dir, logfile)}") + + class Setup: def __init__(self, config={}): self.config = config From 31fa79146f0cec15bd1b96ef50850027abd9c9c9 Mon Sep 17 00:00:00 2001 From: Savely Krendelhoff Date: Wed, 15 Nov 2023 12:30:28 +0300 Subject: [PATCH 2/2] [Chore] Abstract coloured print and log pattern Problem: Lots of times we need to print coloured and log the same message. Solution: Add colorcode argument to the `print_and_log` function. --- baking/src/tezos_baking/tezos_setup_wizard.py | 46 +++++++++---------- .../src/tezos_baking/tezos_voting_wizard.py | 9 ++-- baking/src/tezos_baking/util.py | 1 + baking/src/tezos_baking/wizard_structure.py | 6 +-- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/baking/src/tezos_baking/tezos_setup_wizard.py b/baking/src/tezos_baking/tezos_setup_wizard.py index ce925acd8..ac1ecaacd 100644 --- a/baking/src/tezos_baking/tezos_setup_wizard.py +++ b/baking/src/tezos_baking/tezos_setup_wizard.py @@ -600,37 +600,31 @@ def get_snapshot_metadata(self, name, json_url): snapshot_metadata = self.extract_relevant_snapshot(snapshot_array) if snapshot_metadata is None: - message = f"No suitable snapshot found from the {name} provider." - print( - color( - message, - color_red, - ) + print_and_log( + f"No suitable snapshot found from the {name} provider.", + log=logging.warning, + colorcode=color_yellow, ) - logging.warning(message) else: self.config["snapshots"][name] = snapshot_metadata except urllib.error.URLError: - message = f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n" - print( - color( - message, - color_red, - ) + print_and_log( + f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n", + log=logging.error, + colorcode=color_red, ) - logging.error(message) except ValueError: - message = f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n" - print( - color( - message, - color_red, - ) + print_and_log( + f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n", + log=logging.error, + colorcode=color_red, ) - logging.error(message) except Exception as e: - print_and_log(f"\nUnexpected error handling snapshot metadata:\n{e}\n") + print_and_log( + f"\nUnexpected error handling snapshot metadata:\n{e}\n", + log=logging.error, + ) def output_snapshot_metadata(self, name): from datetime import datetime @@ -1063,9 +1057,11 @@ def main(): + ".service" ) - message = "Error in the Tezos Setup Wizard, exiting." - print("\n" + color(message, color_red)) - logging.error(message) + print_and_log( + "Error in the Tezos Setup Wizard, exiting.", + log=logging.error, + colorcode=color_red, + ) log_exception(exception=e, logfile="tezos-setup.log") diff --git a/baking/src/tezos_baking/tezos_voting_wizard.py b/baking/src/tezos_baking/tezos_voting_wizard.py index 93886723f..86e969187 100644 --- a/baking/src/tezos_baking/tezos_voting_wizard.py +++ b/baking/src/tezos_baking/tezos_voting_wizard.py @@ -536,9 +536,12 @@ def main(): logging.info("Exiting the Tezos Voting Wizard.") sys.exit(1) except Exception as e: - message = "Error in the Tezos Voting Wizard, exiting." - print("\n" + color(message, color_red)) - logging.error(message) + + print_and_log( + "Error in the Tezos Voting Wizard, exiting.", + log=logging.error, + colorcode=color_red, + ) log_exception(exception=e, logfile="tezos-vote.log") diff --git a/baking/src/tezos_baking/util.py b/baking/src/tezos_baking/util.py index 4928cb548..b76620864 100644 --- a/baking/src/tezos_baking/util.py +++ b/baking/src/tezos_baking/util.py @@ -108,6 +108,7 @@ def color(input, colorcode): color_red = "\x1b[1;31m" color_green = "\x1b[1;32m" +color_yellow = "\x1b[1;33m" def yes_or_no(prompt, default=None): diff --git a/baking/src/tezos_baking/wizard_structure.py b/baking/src/tezos_baking/wizard_structure.py index c2ea314ed..bdb953deb 100644 --- a/baking/src/tezos_baking/wizard_structure.py +++ b/baking/src/tezos_baking/wizard_structure.py @@ -107,9 +107,9 @@ def setup_logger(log_file): ) -def print_and_log(s, log=logging.info): - print(s) - log(s) +def print_and_log(message, log=logging.info, colorcode=None): + print(color(message, colorcode) if colorcode else message) + log(message) def log_exception(exception, logfile):