From f8793fbda031928222613c3080ec65584d860984 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 9 Sep 2021 17:21:48 +0200 Subject: [PATCH] [fix] logger per engine: make .logger is always initialized the openstreetmap engine imports code from the wikidata engine. before this commit, specific code make sure to copy the logger variable to the wikidata engine. with this commit searx.engines.load_engine makes sure the .logger is initialized. The implementation scans sys.modules for module name starting with searx.engines. --- searx/engines/__init__.py | 18 +++++++++++++++++- searx/engines/openstreetmap.py | 5 ----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index 154cd605f..e0b5796e4 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -110,10 +110,26 @@ def load_engine(engine_data): if is_missing_required_attributes(engine): return None - engine.logger = logger.getChild(engine_name) + set_loggers(engine, engine_name) + return engine +def set_loggers(engine, engine_name): + # set the logger for engine + engine.logger = logger.getChild(engine_name) + # the engine may have load some other engines + # may sure the logger is initialized + for module_name, module in sys.modules.items(): + if ( + module_name.startswith("searx.engines") + and module_name != "searx.engines.__init__" + and not hasattr(module, "logger") + ): + module_engine_name = module_name.split(".")[-1] + module.logger = logger.getChild(module_engine_name) + + def update_engine_attributes(engine, engine_data): # set engine attributes from engine_data for param_name, param_value in engine_data.items(): diff --git a/searx/engines/openstreetmap.py b/searx/engines/openstreetmap.py index 78c15320a..9f1fe94c9 100644 --- a/searx/engines/openstreetmap.py +++ b/searx/engines/openstreetmap.py @@ -438,8 +438,3 @@ def get_key_label(key_name, lang): if labels is None: return None return get_label(labels, lang) - - -def init(_): - import searx.engines.wikidata # pylint: disable=import-outside-toplevel - searx.engines.wikidata.logger = logger