diff --git a/Makefile b/Makefile index 88edef5cf..3710af4ec 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ run: install sleep 2 ; \ xdg-open http://127.0.0.1:8888/ ; \ ) & - SEARX_DEBUG=1 ./manage pyenv.cmd python ./searx/webapp.py + SEARX_DEBUG=1 ./manage pyenv.cmd python -m searx.webapp PHONY += install uninstall install uninstall: diff --git a/requirements-dev.txt b/requirements-dev.txt index ae60b6c8f..4be587f34 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -17,3 +17,4 @@ linuxdoc==20210324 aiounittest==1.4.0 yamllint==1.26.3 wlc==1.12 +coloredlogs==15.0.1 diff --git a/searx/__init__.py b/searx/__init__.py index 265f926bc..12376b0fd 100644 --- a/searx/__init__.py +++ b/searx/__init__.py @@ -2,13 +2,23 @@ # lint: pylint # pylint: disable=missing-module-docstring +import sys +import os from os.path import dirname, abspath + import logging import searx.unixthreadname import searx.settings_loader from searx.settings_defaults import settings_set_defaults + +# Debug +LOG_FORMAT_DEBUG = '%(levelname)-7s %(name)-30.30s: %(message)s' + +# Production +LOG_FORMAT_PROD = '%(asctime)-15s %(levelname)s:%(name)s: %(message)s' + searx_dir = abspath(dirname(__file__)) searx_parent_dir = abspath(dirname(dirname(__file__))) settings, settings_load_message = searx.settings_loader.load_settings() @@ -16,22 +26,6 @@ settings, settings_load_message = searx.settings_loader.load_settings() if settings is not None: settings = settings_set_defaults(settings) -searx_debug = settings['general']['debug'] -if searx_debug: - logging.basicConfig(level=logging.DEBUG) -else: - logging.basicConfig(level=logging.WARNING) - -logger = logging.getLogger('searx') -logger.info(settings_load_message) - -# log max_request_timeout -max_request_timeout = settings['outgoing']['max_request_timeout'] -if max_request_timeout is None: - logger.info('max_request_timeout=%s', repr(max_request_timeout)) -else: - logger.info('max_request_timeout=%i second(s)', max_request_timeout) - _unset = object() def get_setting(name, default=_unset): @@ -53,3 +47,61 @@ def get_setting(name, default=_unset): break return value + + +def is_color_terminal(): + if os.getenv('TERM') in ('dumb', 'unknown'): + return False + return sys.stdout.isatty() + + +def logging_config_debug(): + try: + import coloredlogs # pylint: disable=import-outside-toplevel + except ImportError: + coloredlogs = None + + log_level = os.environ.get('SEARX_DEBUG_LOG_LEVEL', 'DEBUG') + if coloredlogs and is_color_terminal(): + level_styles = { + 'spam': {'color': 'green', 'faint': True}, + 'debug': {}, + 'notice': {'color': 'magenta'}, + 'success': {'bold': True, 'color': 'green'}, + 'info': {'bold': True, 'color': 'cyan'}, + 'warning': {'color': 'yellow'}, + 'error': {'color': 'red'}, + 'critical': {'bold': True, 'color': 'red'}, + } + field_styles = { + 'asctime': {'color': 'green'}, + 'hostname': {'color': 'magenta'}, + 'levelname': {'color': 8}, + 'name': {'color': 8}, + 'programname': {'color': 'cyan'}, + 'username': {'color': 'yellow'} + } + coloredlogs.install( + level=log_level, + level_styles=level_styles, + field_styles=field_styles, + fmt=LOG_FORMAT_DEBUG + ) + else: + logging.basicConfig(level=logging.getLevelName(log_level), format=LOG_FORMAT_DEBUG) + + +searx_debug = settings['general']['debug'] +if searx_debug: + logging_config_debug() +else: + logging.basicConfig(level=logging.WARNING, format=LOG_FORMAT_PROD) +logger = logging.getLogger('searx') +logger.info(settings_load_message) + +# log max_request_timeout +max_request_timeout = settings['outgoing']['max_request_timeout'] +if max_request_timeout is None: + logger.info('max_request_timeout=%s', repr(max_request_timeout)) +else: + logger.info('max_request_timeout=%i second(s)', max_request_timeout) diff --git a/tests/__init__.py b/tests/__init__.py index d4b101cc4..23e62f040 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,6 +3,7 @@ import os import aiounittest os.environ['SEARX_DEBUG'] = '1' +os.environ['SEARX_DEBUG_LOG_LEVEL'] = 'WARNING' os.environ['SEARX_DISABLE_ETC_SETTINGS'] = '1' os.environ.pop('SEARX_SETTINGS_PATH', None)