mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Support others log categories
This commit is contained in:
parent
930567f54f
commit
89124d4a9f
2 changed files with 52 additions and 29 deletions
|
@ -1614,13 +1614,13 @@ log:
|
||||||
action_help: List logs
|
action_help: List logs
|
||||||
api: GET /logs
|
api: GET /logs
|
||||||
arguments:
|
arguments:
|
||||||
|
categories:
|
||||||
|
help: Log categories to display (default operations)
|
||||||
|
nargs: "*"
|
||||||
-l:
|
-l:
|
||||||
full: --limit
|
full: --limit
|
||||||
help: Maximum number of logs
|
help: Maximum number of logs
|
||||||
type: int
|
type: int
|
||||||
--full:
|
|
||||||
help: Show more details
|
|
||||||
action: store_true
|
|
||||||
|
|
||||||
### log_display()
|
### log_display()
|
||||||
display:
|
display:
|
||||||
|
|
|
@ -27,23 +27,26 @@
|
||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
import errno
|
import errno
|
||||||
|
import collections
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from logging import FileHandler, getLogger, Formatter
|
from logging import FileHandler, getLogger, Formatter
|
||||||
from sys import exc_info
|
from sys import exc_info
|
||||||
|
|
||||||
from moulinette import m18n
|
from moulinette import m18n, msettings
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
from moulinette.utils.log import getActionLogger
|
from moulinette.utils.log import getActionLogger
|
||||||
|
|
||||||
OPERATIONS_PATH = '/var/log/yunohost/operation/'
|
CATEGORIES_PATH = '/var/log/yunohost/categories/'
|
||||||
|
CATEGORIES = ['operation', 'history', 'package', 'system', 'access', 'service', \
|
||||||
|
'app']
|
||||||
METADATA_FILE_EXT = '.yml'
|
METADATA_FILE_EXT = '.yml'
|
||||||
LOG_FILE_EXT = '.log'
|
LOG_FILE_EXT = '.log'
|
||||||
RELATED_CATEGORIES = ['app', 'domain', 'service', 'user']
|
RELATED_CATEGORIES = ['app', 'domain', 'service', 'user']
|
||||||
|
|
||||||
logger = getActionLogger('yunohost.log')
|
logger = getActionLogger('yunohost.log')
|
||||||
|
|
||||||
def log_list(limit=None, full=False):
|
def log_list(categories=[], limit=None):
|
||||||
"""
|
"""
|
||||||
List available logs
|
List available logs
|
||||||
|
|
||||||
|
@ -51,33 +54,45 @@ def log_list(limit=None, full=False):
|
||||||
limit -- Maximum number of logs
|
limit -- Maximum number of logs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
result = {"operations": []}
|
if not categories:
|
||||||
|
is_api = msettings.get('interface') == 'api'
|
||||||
|
categories = ["operation"] if not is_api else CATEGORIES
|
||||||
|
|
||||||
if not os.path.exists(OPERATIONS_PATH):
|
result = collections.OrderedDict()
|
||||||
return result
|
for category in categories:
|
||||||
|
result[category] = []
|
||||||
|
|
||||||
operations = filter(lambda x: x.endswith(METADATA_FILE_EXT), os.listdir(OPERATIONS_PATH))
|
category_path = os.path.join(CATEGORIES_PATH, category)
|
||||||
operations = reversed(sorted(operations))
|
if not os.path.exists(category_path):
|
||||||
|
continue
|
||||||
|
|
||||||
if limit is not None:
|
logs = filter(lambda x: x.endswith(METADATA_FILE_EXT), os.listdir(category_path))
|
||||||
operations = operations[:limit]
|
logs = reversed(sorted(logs))
|
||||||
|
|
||||||
for operation in operations:
|
if limit is not None:
|
||||||
|
logs = logs[:limit]
|
||||||
|
|
||||||
base_filename = operation[:-len(METADATA_FILE_EXT)]
|
for log in logs:
|
||||||
md_filename = operation
|
|
||||||
md_path = os.path.join(OPERATIONS_PATH, md_filename)
|
|
||||||
|
|
||||||
operation = base_filename.split("-")
|
base_filename = log[:-len(METADATA_FILE_EXT)]
|
||||||
|
md_filename = log
|
||||||
|
md_path = os.path.join(category_path, md_filename)
|
||||||
|
|
||||||
operation_datetime = datetime.strptime(" ".join(operation[:2]), "%Y%m%d %H%M%S")
|
log = base_filename.split("-")
|
||||||
|
|
||||||
result["operations"].append({
|
entry = {
|
||||||
"started_at": operation_datetime,
|
"name": base_filename,
|
||||||
"description": m18n.n("log_" + operation[2], *operation[3:]),
|
"path": md_path,
|
||||||
"name": base_filename,
|
}
|
||||||
"path": md_path,
|
try:
|
||||||
})
|
log_datetime = datetime.strptime(" ".join(log[:2]), "%Y%m%d %H%M%S")
|
||||||
|
except ValueError:
|
||||||
|
entry["description"] = m18n.n("log_" + log[0], *log[1:]),
|
||||||
|
else:
|
||||||
|
entry["description"] = m18n.n("log_" + log[2], *log[3:]),
|
||||||
|
entry["started_at"] = log_datetime
|
||||||
|
|
||||||
|
result[category].append(entry)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -98,7 +113,10 @@ def log_display(path, number=50):
|
||||||
abs_path = path
|
abs_path = path
|
||||||
log_path = None
|
log_path = None
|
||||||
if not path.startswith('/'):
|
if not path.startswith('/'):
|
||||||
abs_path = os.path.join(OPERATIONS_PATH, path)
|
for category in CATEGORIES:
|
||||||
|
abs_path = os.path.join(CATEGORIES_PATH, category, path)
|
||||||
|
if os.path.exists(abs_path) or os.path.exists(abs_path + METADATA_FILE_EXT):
|
||||||
|
break
|
||||||
|
|
||||||
if os.path.exists(abs_path) and not path.endswith(METADATA_FILE_EXT) :
|
if os.path.exists(abs_path) and not path.endswith(METADATA_FILE_EXT) :
|
||||||
log_path = abs_path
|
log_path = abs_path
|
||||||
|
@ -116,9 +134,14 @@ def log_display(path, number=50):
|
||||||
infos = {}
|
infos = {}
|
||||||
|
|
||||||
# If it's a unit operation, display the name and the description
|
# If it's a unit operation, display the name and the description
|
||||||
if base_path.startswith(OPERATIONS_PATH):
|
if base_path.startswith(CATEGORIES_PATH):
|
||||||
operation = base_filename.split("-")
|
log = base_filename.split("-")
|
||||||
infos['description'] = m18n.n("log_" + operation[2], *operation[3:]),
|
try:
|
||||||
|
datetime.strptime(" ".join(log[:2]), "%Y%m%d %H%M%S")
|
||||||
|
except ValueError:
|
||||||
|
infos["description"] = m18n.n("log_" + log[0], *log[1:]),
|
||||||
|
else:
|
||||||
|
infos["description"] = m18n.n("log_" + log[2], *log[3:]),
|
||||||
infos['name'] = base_filename
|
infos['name'] = base_filename
|
||||||
|
|
||||||
# Display metadata if exist
|
# Display metadata if exist
|
||||||
|
|
Loading…
Add table
Reference in a new issue