From 10576f7138d287dad56ebc18efec2992198f51bb Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 31 Dec 2020 14:02:37 +0100 Subject: [PATCH 1/7] [mod] unify all subloggers under moulinette.core --- moulinette/actionsmap.py | 2 +- moulinette/authenticators/__init__.py | 2 +- moulinette/authenticators/dummy.py | 2 +- moulinette/authenticators/ldap.py | 2 +- moulinette/interfaces/__init__.py | 2 +- moulinette/interfaces/api.py | 2 +- moulinette/interfaces/cli.py | 2 +- moulinette/utils/serialize.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index d36c59f7..1b716074 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -18,7 +18,7 @@ from moulinette.core import MoulinetteError, MoulinetteLock from moulinette.interfaces import BaseActionsMapParser, GLOBAL_SECTION, TO_RETURN_PROP from moulinette.utils.log import start_action_logging -logger = logging.getLogger("moulinette.actionsmap") +logger = logging.getLogger("moulinette.core") # Extra parameters ---------------------------------------------------- diff --git a/moulinette/authenticators/__init__.py b/moulinette/authenticators/__init__.py index 0170d345..1db09f4d 100644 --- a/moulinette/authenticators/__init__.py +++ b/moulinette/authenticators/__init__.py @@ -8,7 +8,7 @@ import hmac from moulinette.cache import open_cachefile, get_cachedir, cachefile_exists from moulinette.core import MoulinetteError -logger = logging.getLogger("moulinette.authenticator") +logger = logging.getLogger("moulinette.core") # Base Class ----------------------------------------------------------- diff --git a/moulinette/authenticators/dummy.py b/moulinette/authenticators/dummy.py index e2978d12..b397b854 100644 --- a/moulinette/authenticators/dummy.py +++ b/moulinette/authenticators/dummy.py @@ -4,7 +4,7 @@ import logging from moulinette.core import MoulinetteError from moulinette.authenticators import BaseAuthenticator -logger = logging.getLogger("moulinette.authenticator.dummy") +logger = logging.getLogger("moulinette.core") # Dummy authenticator implementation diff --git a/moulinette/authenticators/ldap.py b/moulinette/authenticators/ldap.py index 13c635a3..a1ecb43d 100644 --- a/moulinette/authenticators/ldap.py +++ b/moulinette/authenticators/ldap.py @@ -13,7 +13,7 @@ from moulinette import m18n from moulinette.core import MoulinetteError, MoulinetteLdapIsDownError from moulinette.authenticators import BaseAuthenticator -logger = logging.getLogger("moulinette.authenticator.ldap") +logger = logging.getLogger("moulinette.core") # LDAP Class Implementation -------------------------------------------- diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index a73bc5f6..77851b07 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -9,7 +9,7 @@ from collections import deque, OrderedDict from moulinette import msettings, m18n from moulinette.core import MoulinetteError -logger = logging.getLogger("moulinette.interface") +logger = logging.getLogger("moulinette.core") GLOBAL_SECTION = "_global" TO_RETURN_PROP = "_to_return" diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 5d3d170c..e2103260 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -460,7 +460,7 @@ class _ActionsMapPlugin(object): # Delete the current queue and break del self.log_queues[s_id] break - logger.exception("invalid item in the messages queue: %r", item) + logger.error("invalid item in the messages queue: %r", item) else: try: # Send the message diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 19505365..0c36804b 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -53,7 +53,7 @@ def monkey_get_action_name(argument): argparse._get_action_name = monkey_get_action_name -logger = log.getLogger("moulinette.cli") +logger = log.getLogger("moulinette.core") # CLI helpers ---------------------------------------------------------- diff --git a/moulinette/utils/serialize.py b/moulinette/utils/serialize.py index 345cb4d4..3446c467 100644 --- a/moulinette/utils/serialize.py +++ b/moulinette/utils/serialize.py @@ -2,7 +2,7 @@ import logging from json.encoder import JSONEncoder import datetime -logger = logging.getLogger("moulinette.utils.serialize") +logger = logging.getLogger("moulinette.core") # JSON utilities ------------------------------------------------------- From 9a35d77a0852fd83a32466d160d8e59bbb4f7236 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 31 Dec 2020 19:47:08 +0100 Subject: [PATCH 2/7] [mod] better error message --- moulinette/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/moulinette/core.py b/moulinette/core.py index 0f735386..982c2fff 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -39,7 +39,11 @@ class Translator(object): # Attempt to load default translations if not self._load_translations(default_locale): logger.error( - "unable to load locale '%s' from '%s'", default_locale, locale_dir + "unable to load locale '%s' from '%s'. Does the file '%s/%s.json' exists?", + default_locale, + locale_dir, + locale_dir, + default_locale, ) self.default_locale = default_locale From e7a01c68f5d05874daca6e5747112aedee54f023 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 31 Jan 2021 16:06:33 +0100 Subject: [PATCH 3/7] Revert "[mod] unify all subloggers under moulinette.core" This reverts commit c88f127b8c691c6121229402be4375fdc62ed6bf. --- moulinette/actionsmap.py | 2 +- moulinette/authenticators/__init__.py | 2 +- moulinette/authenticators/dummy.py | 2 +- moulinette/authenticators/ldap.py | 2 +- moulinette/interfaces/__init__.py | 2 +- moulinette/interfaces/api.py | 2 +- moulinette/interfaces/cli.py | 2 +- moulinette/utils/serialize.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index 1b716074..d36c59f7 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -18,7 +18,7 @@ from moulinette.core import MoulinetteError, MoulinetteLock from moulinette.interfaces import BaseActionsMapParser, GLOBAL_SECTION, TO_RETURN_PROP from moulinette.utils.log import start_action_logging -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.actionsmap") # Extra parameters ---------------------------------------------------- diff --git a/moulinette/authenticators/__init__.py b/moulinette/authenticators/__init__.py index 1db09f4d..0170d345 100644 --- a/moulinette/authenticators/__init__.py +++ b/moulinette/authenticators/__init__.py @@ -8,7 +8,7 @@ import hmac from moulinette.cache import open_cachefile, get_cachedir, cachefile_exists from moulinette.core import MoulinetteError -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.authenticator") # Base Class ----------------------------------------------------------- diff --git a/moulinette/authenticators/dummy.py b/moulinette/authenticators/dummy.py index b397b854..e2978d12 100644 --- a/moulinette/authenticators/dummy.py +++ b/moulinette/authenticators/dummy.py @@ -4,7 +4,7 @@ import logging from moulinette.core import MoulinetteError from moulinette.authenticators import BaseAuthenticator -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.authenticator.dummy") # Dummy authenticator implementation diff --git a/moulinette/authenticators/ldap.py b/moulinette/authenticators/ldap.py index a1ecb43d..13c635a3 100644 --- a/moulinette/authenticators/ldap.py +++ b/moulinette/authenticators/ldap.py @@ -13,7 +13,7 @@ from moulinette import m18n from moulinette.core import MoulinetteError, MoulinetteLdapIsDownError from moulinette.authenticators import BaseAuthenticator -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.authenticator.ldap") # LDAP Class Implementation -------------------------------------------- diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index 77851b07..a73bc5f6 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -9,7 +9,7 @@ from collections import deque, OrderedDict from moulinette import msettings, m18n from moulinette.core import MoulinetteError -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.interface") GLOBAL_SECTION = "_global" TO_RETURN_PROP = "_to_return" diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index e2103260..5d3d170c 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -460,7 +460,7 @@ class _ActionsMapPlugin(object): # Delete the current queue and break del self.log_queues[s_id] break - logger.error("invalid item in the messages queue: %r", item) + logger.exception("invalid item in the messages queue: %r", item) else: try: # Send the message diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 0c36804b..19505365 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -53,7 +53,7 @@ def monkey_get_action_name(argument): argparse._get_action_name = monkey_get_action_name -logger = log.getLogger("moulinette.core") +logger = log.getLogger("moulinette.cli") # CLI helpers ---------------------------------------------------------- diff --git a/moulinette/utils/serialize.py b/moulinette/utils/serialize.py index 3446c467..345cb4d4 100644 --- a/moulinette/utils/serialize.py +++ b/moulinette/utils/serialize.py @@ -2,7 +2,7 @@ import logging from json.encoder import JSONEncoder import datetime -logger = logging.getLogger("moulinette.core") +logger = logging.getLogger("moulinette.utils.serialize") # JSON utilities ------------------------------------------------------- From bdd048197ddb0561366244ac8629a3de1e1b0650 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 31 Jan 2021 16:08:50 +0100 Subject: [PATCH 4/7] Misc logging fixes --- moulinette/__init__.py | 6 +++--- moulinette/actionsmap.py | 4 ++-- moulinette/interfaces/api.py | 2 +- moulinette/utils/log.py | 1 - 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/moulinette/__init__.py b/moulinette/__init__.py index 29f9bb98..625217f8 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -90,12 +90,12 @@ def api(host="localhost", port=80, routes={}): except MoulinetteError as e: import logging - logging.getLogger(logging.main_logger).error(e.strerror) + logging.getLogger("moulinette").error(e.strerror) return 1 except KeyboardInterrupt: import logging - logging.getLogger(logging.main_logger).info(m18n.g("operation_interrupted")) + logging.getLogger("moulinette").info(m18n.g("operation_interrupted")) return 0 @@ -122,7 +122,7 @@ def cli(args, top_parser, output_as=None, timeout=None): except MoulinetteError as e: import logging - logging.getLogger(logging.main_logger).error(e.strerror) + logging.getLogger("moulinette").error(e.strerror) return 1 return 0 diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index d36c59f7..d4a5f079 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -577,14 +577,14 @@ class ActionsMap(object): log_id = start_action_logging() if logger.isEnabledFor(logging.DEBUG): # Log arguments in debug mode only for safety reasons - logger.info( + logger.debug( "processing action [%s]: %s with args=%s", log_id, full_action_name, arguments, ) else: - logger.info("processing action [%s]: %s", log_id, full_action_name) + logger.debug("processing action [%s]: %s", log_id, full_action_name) # Load translation and process the action m18n.load_namespace(namespace) diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 5d3d170c..a3427a2b 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -208,7 +208,7 @@ class _HTTPArgumentParser(object): def _error(self, message): # TODO: Raise a proper exception - raise MoulinetteError(message) + raise MoulinetteError(message, raw_msg=True) class _ActionsMapPlugin(object): diff --git a/moulinette/utils/log.py b/moulinette/utils/log.py index 65006d2e..375affc2 100644 --- a/moulinette/utils/log.py +++ b/moulinette/utils/log.py @@ -65,7 +65,6 @@ def configure_logging(logging_config=None): # load configuration from dict dictConfig(DEFAULT_LOGGING) if logging_config: - logging.main_logger = logging_config.get("main_logger") dictConfig(logging_config) From cde45de0bae84ef2313be501b06597cc4a22f958 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 28 Feb 2021 16:54:19 +0100 Subject: [PATCH 5/7] Add m18n helper to check that a key exist --- moulinette/core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/moulinette/core.py b/moulinette/core.py index 982c2fff..81a4754f 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -86,6 +86,9 @@ class Translator(object): self.locale = locale return True + def key_exists(self, key): + return key in self._translations[self.default_locale] + def translate(self, key, *args, **kwargs): """Retrieve proper translation for a key @@ -258,6 +261,15 @@ class Moulinette18n(object): """ return self._namespaces[self._current_namespace].translate(key, *args, **kwargs) + def key_exists(self, key): + """Check if a key exists in the translation files + + Keyword arguments: + - key -- The key to translate + + """ + return self._namespaces[self._current_namespace].key_exists(key) + class MoulinetteSignals(object): From 16c43a67f763bb3aa173a864e95df4a537c76d80 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 28 Feb 2021 16:54:54 +0100 Subject: [PATCH 6/7] Only report missing/failed translation as a warning, no need to report as exception... --- moulinette/core.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index 81a4754f..409c6b61 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -103,7 +103,7 @@ class Translator(object): if key in self._translations.get(self.locale, {}): try: return self._translations[self.locale][key].format(*args, **kwargs) - except KeyError as e: + except Exception as e: unformatted_string = self._translations[self.locale][key] error_message = ( "Failed to format translated string '%s': '%s' with arguments '%s' and '%s, raising error: %s(%s) (don't panic this is just a warning)" @@ -111,7 +111,7 @@ class Translator(object): ) if not during_unittests_run(): - logger.exception(error_message) + logger.warning(error_message) else: raise Exception(error_message) @@ -127,14 +127,14 @@ class Translator(object): return self._translations[self.default_locale][key].format( *args, **kwargs ) - except KeyError as e: + except Exception as e: unformatted_string = self._translations[self.default_locale][key] error_message = ( "Failed to format translatable string '%s': '%s' with arguments '%s' and '%s', raising error: %s(%s) (don't panic this is just a warning)" % (key, unformatted_string, args, kwargs, e.__class__.__name__, e) ) if not during_unittests_run(): - logger.exception(error_message) + logger.warning(error_message) else: raise Exception(error_message) @@ -146,7 +146,7 @@ class Translator(object): ) if not during_unittests_run(): - logger.exception(error_message) + logger.warning(error_message) else: raise Exception(error_message) From 9670357f987edb37360a82542f34e0b2665c81d5 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 28 Feb 2021 16:58:50 +0100 Subject: [PATCH 7/7] No need to display this message --- moulinette/core.py | 1 - 1 file changed, 1 deletion(-) diff --git a/moulinette/core.py b/moulinette/core.py index 409c6b61..c41b7d21 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -121,7 +121,6 @@ class Translator(object): self.default_locale != self.locale and key in self._translations.get(self.default_locale, {}) ): - logger.info("untranslated key '%s' for locale '%s'", key, self.locale) try: return self._translations[self.default_locale][key].format(