Skip to content

Commit

Permalink
✨ Enhance log report script: add error handling and logging (#2232)
Browse files Browse the repository at this point in the history
* Update log_example_reports.py

1. Added logging: Imported the logging module and set up a logger in the main function. This allows for better error tracking and debugging.

2. Improved file reading: Used a with statement to ensure the file is properly closed after reading. Also added error handling to catch and log any issues when reading the file.

3. Error handling for Slack SDK import: Added a try-except block to handle cases where the slack_sdk might not be installed.

4. Enhanced Slack message sending: Added error handling and logging for the Slack message sending process. This will help identify any issues with the Slack integration.

* style

* Update log_reports.py

1. Logging: Added logging to track errors and important events.

2. Error Handling: Wrapped the log file processing in a try-except block to handle potential errors gracefully.

3. Logging Total Failed Tests: Added a log statement to report the total number of failed tests

* style

* further improve

---------

Co-authored-by: Quentin Gallouédec <[email protected]>
Co-authored-by: Quentin Gallouédec <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent a67f214 commit b9aa965
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions scripts/log_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import argparse
import json
import logging
import os
from datetime import date
from pathlib import Path
Expand All @@ -25,6 +26,9 @@
parser = argparse.ArgumentParser()
parser.add_argument("--slack_channel_name", default="trl-push-ci")

# Set up logging
logging.basicConfig(level=logging.INFO)


def main(slack_channel_name=None):
failed = []
Expand All @@ -40,25 +44,31 @@ def main(slack_channel_name=None):
for log in Path().glob("*.log"):
section_num_failed = 0
i = 0
with open(log) as f:
for line in f:
line = json.loads(line)
i += 1
if line.get("nodeid", "") != "":
test = line["nodeid"]
if line.get("duration", None) is not None:
duration = f'{line["duration"]:.4f}'
if line.get("outcome", "") == "failed":
section_num_failed += 1
failed.append([test, duration, log.name.split("_")[0]])
total_num_failed += 1
else:
passed.append([test, duration, log.name.split("_")[0]])
try: # Added error handling for file operations
with open(log) as f:
for line in f:
line = json.loads(line)
i += 1
if line.get("nodeid", "") != "":
test = line["nodeid"]
if line.get("duration", None) is not None:
duration = f'{line["duration"]:.4f}'
if line.get("outcome", "") == "failed":
section_num_failed += 1
failed.append([test, duration, log.name.split("_")[0]])
total_num_failed += 1
else:
passed.append([test, duration, log.name.split("_")[0]])
empty_file = i == 0
group_info.append([str(log), section_num_failed, failed])
total_empty_files.append(empty_file)
os.remove(log)
failed = []
except Exception as e: # Catch any exceptions during file processing
logging.error(f"Error processing log file {log}: {e}")
else:
group_info.append([str(log), section_num_failed, failed])
total_empty_files.append(empty_file)
finally:
os.remove(log)
failed = []

no_error_payload = {
"type": "section",
"text": {
Expand Down Expand Up @@ -104,6 +114,7 @@ def main(slack_channel_name=None):

if total_empty_files[i]:
message += f"\n*{name}: Warning! Empty file - please check the GitHub action job *\n"
logging.info(f"Total failed tests: {total_num_failed}") # Log the total failed tests
print(f"### {message}")
else:
payload.append(no_error_payload)
Expand Down

0 comments on commit b9aa965

Please sign in to comment.