Try to show smarter / more useful logs by filtering irrelevant lines like 'set +x' etc

This commit is contained in:
Alexandre Aubin 2020-04-26 20:49:09 +02:00
parent aaccb54775
commit 1cb330823d
4 changed files with 47 additions and 3 deletions

View file

@ -1659,6 +1659,10 @@ log:
--share: --share:
help: Share the full log using yunopaste help: Share the full log using yunopaste
action: store_true 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
############################# #############################

View file

@ -914,6 +914,19 @@ def dump_app_log_extract_for_debugging(operation_logger):
with open(operation_logger.log_path, "r") as f: with open(operation_logger.log_path, "r") as f:
lines = f.readlines() 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 = [] lines_to_display = []
for line in lines: 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 # 2019-10-19 16:10:27,611: DEBUG - + mysql -u piwigo --password=********** -B piwigo
# And we just want the part starting by "DEBUG - " # And we just want the part starting by "DEBUG - "
line = line.strip().split(": ", 1)[1] line = line.strip().split(": ", 1)[1]
if any(filter_.search(line) for filter_ in filters):
continue
lines_to_display.append(line) lines_to_display.append(line)
if line.endswith("+ ynh_exit_properly") or " + ynh_die " in line: if line.endswith("+ ynh_exit_properly") or " + ynh_die " in line:

View file

@ -122,7 +122,7 @@ def log_list(category=[], limit=None, with_details=False):
return result 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. 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 # Display logs if exist
if os.path.exists(log_path): 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 from yunohost.service import _tail
if number: if number:
logs = _tail(log_path, int(number)) logs = _tail(log_path, int(number), filters=filters)
else: else:
logs = read_file(log_path) logs = read_file(log_path)
infos['log_path'] = log_path infos['log_path'] = log_path

View file

@ -23,6 +23,8 @@
Manage services Manage services
""" """
import re
import os import os
import re import re
import time import time
@ -616,7 +618,7 @@ def _save_services(services):
raise raise
def _tail(file, n): def _tail(file, n, filters=[]):
""" """
Reads a n lines from f with an offset of offset lines. The return 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 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 avg_line_length = 74
to_read = n to_read = n
if filters:
filters = [re.compile(f) for f in filters]
try: try:
if file.endswith(".gz"): if file.endswith(".gz"):
import gzip import gzip
@ -647,6 +652,9 @@ def _tail(file, n):
pos = f.tell() pos = f.tell()
lines = f.read().splitlines() lines = f.read().splitlines()
for filter_ in filters:
lines = [l for l in lines if not filter_.search(l)]
if len(lines) >= to_read: if len(lines) >= to_read:
return lines[-to_read:] return lines[-to_read:]