Merge pull request #716 from YunoHost/improve-log-list

Add a --with-details option for log list
This commit is contained in:
Alexandre Aubin 2019-05-02 14:54:52 +02:00 committed by GitHub
commit a6ac514202
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -1802,6 +1802,10 @@ log:
full: --limit
help: Maximum number of logs
type: int
-d:
full: --with-details
help: Show additional infos (e.g. operation success) but may significantly increase command time. Consider using --limit in combination with this.
action: store_true
### log_display()
display:

View file

@ -47,12 +47,13 @@ RELATED_CATEGORIES = ['app', 'domain', 'service', 'user']
logger = getActionLogger('yunohost.log')
def log_list(category=[], limit=None):
def log_list(category=[], limit=None, with_details=False):
"""
List available logs
Keyword argument:
limit -- Maximum number of logs
with_details -- Include details (e.g. if the operation was a success). Likely to increase the command time as it needs to open and parse the metadata file for each log... So try to use this in combination with --limit.
"""
categories = category
@ -69,12 +70,11 @@ def log_list(category=[], limit=None):
category_path = os.path.join(CATEGORIES_PATH, category)
if not os.path.exists(category_path):
logger.debug(m18n.n('log_category_404', category=category))
continue
logs = filter(lambda x: x.endswith(METADATA_FILE_EXT),
os.listdir(category_path))
logs = reversed(sorted(logs))
logs = list(reversed(sorted(logs)))
if limit is not None:
logs = logs[:limit]
@ -100,6 +100,15 @@ def log_list(category=[], limit=None):
else:
entry["started_at"] = log_datetime
if with_details:
with open(md_path, "r") as md_file:
try:
metadata = yaml.safe_load(md_file)
except yaml.YAMLError:
logger.warning(m18n.n('log_corrupted_md_file', file=md_path))
entry["success"] = metadata.get("success", "?")
result[category].append(entry)
# Reverse the order of log when in cli, more comfortable to read (avoid
@ -318,6 +327,20 @@ class OperationLogger(object):
self.flush()
self._register_log()
@property
def md_path(self):
"""
Metadata path file
"""
return os.path.join(self.path, self.name + METADATA_FILE_EXT)
@property
def log_path(self):
"""
Log path file
"""
return os.path.join(self.path, self.name + LOG_FILE_EXT)
def _register_log(self):
"""
Register log with a handler connected on log system