From 1cb330823d3e1348fc1e350105729d6604890648 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 26 Apr 2020 20:49:09 +0200 Subject: [PATCH] Try to show smarter / more useful logs by filtering irrelevant lines like 'set +x' etc --- data/actionsmap/yunohost.yml | 4 ++++ src/yunohost/app.py | 17 +++++++++++++++++ src/yunohost/log.py | 19 +++++++++++++++++-- src/yunohost/service.py | 10 +++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 0ad1268f2..6ccd5ebfe 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -1659,6 +1659,10 @@ log: --share: help: Share the full log using yunopaste action: store_true + -f: + full: --filter-irrelevant + help: Do not show some lines deemed not relevant (like set +x or helper argument parsing) + action: store_true ############################# diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 8a29f9dbb..ba3ac4c01 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -914,6 +914,19 @@ def dump_app_log_extract_for_debugging(operation_logger): with open(operation_logger.log_path, "r") as f: lines = f.readlines() + filters = [ + r"set [+-]x$", + r"local \w+$", + r"local legacy_args=.*$", + r".*Helper used in legacy mode.*", + r"args_array=.*$", + r"declare -Ar args_array$", + r"ynh_handle_getopts_args", + r"ynh_script_progression" + ] + + filters = [re.compile(f) for f in filters] + lines_to_display = [] for line in lines: @@ -924,6 +937,10 @@ def dump_app_log_extract_for_debugging(operation_logger): # 2019-10-19 16:10:27,611: DEBUG - + mysql -u piwigo --password=********** -B piwigo # And we just want the part starting by "DEBUG - " line = line.strip().split(": ", 1)[1] + + if any(filter_.search(line) for filter_ in filters): + continue + lines_to_display.append(line) if line.endswith("+ ynh_exit_properly") or " + ynh_die " in line: diff --git a/src/yunohost/log.py b/src/yunohost/log.py index cd08bdfe0..523a10f76 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -122,7 +122,7 @@ def log_list(category=[], limit=None, with_details=False): return result -def log_display(path, number=None, share=False): +def log_display(path, number=None, share=False, filter_irrelevant=False): """ Display a log file enriched with metadata if any. @@ -202,9 +202,24 @@ def log_display(path, number=None, share=False): # Display logs if exist if os.path.exists(log_path): + + if filter_irrelevant: + filters = [ + r"set [+-]x$", + r"local \w+$", + r"local legacy_args=.*$", + r".*Helper used in legacy mode.*", + r"args_array=.*$", + r"declare -Ar args_array$", + r"ynh_handle_getopts_args", + r"ynh_script_progression" + ] + else: + filters = [] + from yunohost.service import _tail if number: - logs = _tail(log_path, int(number)) + logs = _tail(log_path, int(number), filters=filters) else: logs = read_file(log_path) infos['log_path'] = log_path diff --git a/src/yunohost/service.py b/src/yunohost/service.py index 2f45e28c3..c17eb04c2 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -23,6 +23,8 @@ Manage services """ + +import re import os import re import time @@ -616,7 +618,7 @@ def _save_services(services): raise -def _tail(file, n): +def _tail(file, n, filters=[]): """ Reads a n lines from f with an offset of offset lines. The return value is a tuple in the form ``(lines, has_more)`` where `has_more` is @@ -627,6 +629,9 @@ def _tail(file, n): avg_line_length = 74 to_read = n + if filters: + filters = [re.compile(f) for f in filters] + try: if file.endswith(".gz"): import gzip @@ -647,6 +652,9 @@ def _tail(file, n): pos = f.tell() lines = f.read().splitlines() + for filter_ in filters: + lines = [l for l in lines if not filter_.search(l)] + if len(lines) >= to_read: return lines[-to_read:]