From cf560b8f91b4221d0d4363093f6a1dc8d0e96704 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 31 Dec 2020 14:05:39 +0100 Subject: [PATCH 1/5] [mod] activate moulinette.core logging in cli --- src/yunohost/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/yunohost/__init__.py b/src/yunohost/__init__.py index 76449a7e4..0c3cb8355 100644 --- a/src/yunohost/__init__.py +++ b/src/yunohost/__init__.py @@ -143,6 +143,11 @@ def init_logging(interface="cli", 'handlers': ['file', 'tty'] if not quiet else ['file'], 'propagate': False, }, + 'moulinette.core': { + 'level': 'DEBUG' if debug else 'ERROR', + 'handlers': ['file', 'tty'] if not quiet else ['file'], + 'propagate': False, + }, }, 'root': { 'level': 'DEBUG', From c931f8a5323a9c2e734a0764330d03f73dd87dfc Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 31 Dec 2020 14:05:58 +0100 Subject: [PATCH 2/5] [mod] activate moulinette.core and moulinette.interface.api logging in api --- src/yunohost/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/yunohost/__init__.py b/src/yunohost/__init__.py index 0c3cb8355..bda2cb994 100644 --- a/src/yunohost/__init__.py +++ b/src/yunohost/__init__.py @@ -203,6 +203,16 @@ def init_logging(interface="cli", 'handlers': [], 'propagate': True, }, + 'moulinette.core': { + 'level': 'DEBUG' if debug else 'ERROR', + 'handlers': ['file', 'tty'] if not quiet else ['file'], + 'propagate': False, + }, + 'moulinette.interface.api': { + 'level': 'DEBUG', + 'handlers': [], + 'propagate': True, + }, }, 'root': { 'level': 'DEBUG', From fcf9e58ce4d164dc2229664964056d6283fa2ace Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 31 Jan 2021 16:01:38 +0100 Subject: [PATCH 3/5] Rework logging configuration --- src/yunohost/__init__.py | 195 +++++++++++++++------------------------ 1 file changed, 74 insertions(+), 121 deletions(-) diff --git a/src/yunohost/__init__.py b/src/yunohost/__init__.py index bda2cb994..29c4b7bb7 100644 --- a/src/yunohost/__init__.py +++ b/src/yunohost/__init__.py @@ -93,129 +93,82 @@ def init_logging(interface="cli", if not os.path.isdir(logdir): os.makedirs(logdir, 0o750) - # ####################################################################### # + logging_configuration = { + 'version': 1, + 'disable_existing_loggers': True, + 'formatters': { + 'console': { + 'format': '%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' + }, + 'tty-debug': { + 'format': '%(relativeCreated)-4d %(fmessage)s' + }, + 'precise': { + 'format': '%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' + }, + }, + 'filters': { + 'action': { + '()': 'moulinette.utils.log.ActionFilter', + }, + }, + 'handlers': { + 'cli': { + 'level': 'DEBUG' if debug else 'INFO', + 'class': 'moulinette.interfaces.cli.TTYHandler', + 'formatter': 'tty-debug' if debug else '', + }, + 'api': { + 'level': 'DEBUG' if debug else 'INFO', + 'class': 'moulinette.interfaces.api.APIQueueHandler', + }, + 'file': { + 'class': 'logging.FileHandler', + 'formatter': 'precise', + 'filename': logfile, + 'filters': ['action'], + }, + }, + 'loggers': { + 'yunohost': { + 'level': 'DEBUG', + 'handlers': ['file', interface] if not quiet else ['file'], + 'propagate': False, + }, + 'moulinette': { + 'level': 'DEBUG', + 'handlers': ['file', interface] if not quiet else ['file'], + 'propagate': False, + }, + }, + 'root': { + 'level': 'DEBUG', + 'handlers': ['file', interface] if debug else ['file'], + }, + } + # Logging configuration for CLI (or any other interface than api...) # - # ####################################################################### # if interface != "api": - configure_logging({ - 'version': 1, - 'main_logger': "yunohost", - 'disable_existing_loggers': True, - 'formatters': { - 'tty-debug': { - 'format': '%(relativeCreated)-4d %(fmessage)s' - }, - 'precise': { - 'format': '%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' - }, - }, - 'filters': { - 'action': { - '()': 'moulinette.utils.log.ActionFilter', - }, - }, - 'handlers': { - 'tty': { - 'level': 'DEBUG' if debug else 'INFO', - 'class': 'moulinette.interfaces.cli.TTYHandler', - 'formatter': 'tty-debug' if debug else '', - }, - 'file': { - 'class': 'logging.FileHandler', - 'formatter': 'precise', - 'filename': logfile, - 'filters': ['action'], - }, - }, - 'loggers': { - 'yunohost': { - 'level': 'DEBUG', - 'handlers': ['file', 'tty'] if not quiet else ['file'], - 'propagate': False, - }, - 'moulinette': { - 'level': 'DEBUG', - 'handlers': [], - 'propagate': True, - }, - 'moulinette.interface': { - 'level': 'DEBUG', - 'handlers': ['file', 'tty'] if not quiet else ['file'], - 'propagate': False, - }, - 'moulinette.core': { - 'level': 'DEBUG' if debug else 'ERROR', - 'handlers': ['file', 'tty'] if not quiet else ['file'], - 'propagate': False, - }, - }, - 'root': { - 'level': 'DEBUG', - 'handlers': ['file', 'tty'] if debug else ['file'], - }, - }) - # ####################################################################### # + + logging_configuration["main_logger"] = "yunohost" + + configure_logging(logging_configuration) + # Logging configuration for API # - # ####################################################################### # else: - configure_logging({ - 'version': 1, - 'disable_existing_loggers': True, - 'formatters': { - 'console': { - 'format': '%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' - }, - 'precise': { - 'format': '%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' - }, + # We use a WatchedFileHandler instead of regular FileHandler to possibly support log rotation etc + logging_configuration["handlers"]["file"]["class"] = 'logging.handlers.WatchedFileHandler' + + # This is for when launching yunohost-api in debug mode, we want to display stuff in the console + if debug: + logging_configuration["handlers"]["console"] = { + 'class': 'logging.StreamHandler', + 'formatter': 'console', + 'stream': 'ext://sys.stdout', + 'filters': ['action'], }, - 'filters': { - 'action': { - '()': 'moulinette.utils.log.ActionFilter', - }, - }, - 'handlers': { - 'api': { - 'level': 'DEBUG' if debug else 'INFO', - 'class': 'moulinette.interfaces.api.APIQueueHandler', - }, - 'file': { - 'class': 'logging.handlers.WatchedFileHandler', - 'formatter': 'precise', - 'filename': logfile, - 'filters': ['action'], - }, - 'console': { - 'class': 'logging.StreamHandler', - 'formatter': 'console', - 'stream': 'ext://sys.stdout', - 'filters': ['action'], - }, - }, - 'loggers': { - 'yunohost': { - 'level': 'DEBUG', - 'handlers': ['file', 'api'] + (['console'] if debug else []), - 'propagate': False, - }, - 'moulinette': { - 'level': 'DEBUG', - 'handlers': [], - 'propagate': True, - }, - 'moulinette.core': { - 'level': 'DEBUG' if debug else 'ERROR', - 'handlers': ['file', 'tty'] if not quiet else ['file'], - 'propagate': False, - }, - 'moulinette.interface.api': { - 'level': 'DEBUG', - 'handlers': [], - 'propagate': True, - }, - }, - 'root': { - 'level': 'DEBUG', - 'handlers': ['file'] + (['console'] if debug else []), - }, - }) + logging_configuration["loggers"]["yunohost"]["handlers"].append("console") + logging_configuration["loggers"]["moulinette"]["handlers"].append("console") + logging_configuration["root"]["handlers"].append("console") + + configure_logging(logging_configuration) From 2ab330a0e2a1c699a6d09510b18b73505f821d11 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 31 Jan 2021 16:08:07 +0100 Subject: [PATCH 4/5] I think we don't need this hack anymore --- src/yunohost/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/yunohost/__init__.py b/src/yunohost/__init__.py index 29c4b7bb7..c536ba255 100644 --- a/src/yunohost/__init__.py +++ b/src/yunohost/__init__.py @@ -149,9 +149,6 @@ def init_logging(interface="cli", # Logging configuration for CLI (or any other interface than api...) # if interface != "api": - - logging_configuration["main_logger"] = "yunohost" - configure_logging(logging_configuration) # Logging configuration for API # From 4131ddb0706ccd6121e0d542cc2b6d84c665f0ab Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 28 Feb 2021 17:00:48 +0100 Subject: [PATCH 5/5] Fix cases where we want to test if translation exists for a key --- src/yunohost/diagnosis.py | 3 +-- src/yunohost/service.py | 11 ++++------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/yunohost/diagnosis.py b/src/yunohost/diagnosis.py index 93ece21fc..5666afb07 100644 --- a/src/yunohost/diagnosis.py +++ b/src/yunohost/diagnosis.py @@ -449,9 +449,8 @@ class Diagnoser(): @staticmethod def get_description(id_): key = "diagnosis_description_" + id_ - descr = m18n.n(key) # If no description available, fallback to id - return descr if descr != key else id_ + return m18n.n(key) if m18n.key_exists(key) else id_ @staticmethod def i18n(report, force_remove_html_tags=False): diff --git a/src/yunohost/service.py b/src/yunohost/service.py index 347932add..211d7bf56 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -352,14 +352,11 @@ def _get_and_format_service_status(service, infos): # If no description was there, try to get it from the .json locales if not description: - translation_key = "service_description_%s" % service - description = m18n.n(translation_key) - # If descrption is still equal to the translation key, - # that mean that we don't have a translation for this string - # that's the only way to test for that for now - # if we don't have it, uses the one provided by systemd - if description == translation_key: + translation_key = "service_description_%s" % service + if m18n.key_exists(translation_key): + description = m18n.key_exists(translation_key) + else: description = str(raw_status.get("Description", "")) output = {