summaryrefslogtreecommitdiff
path: root/searx/__init__.py
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2021-09-06 19:47:50 +0200
committerAlexandre Flament <alex@al-f.net>2021-09-10 21:49:34 +0200
commit577998106aed4c3df5cda8f6af77d55dda8ef60c (patch)
treedb79fe4346ffbbadaa1995b12d321e8c4057c260 /searx/__init__.py
parentb513917ef94d2f18e6db3228363f8089ce1ba08a (diff)
downloadsearxng-577998106aed4c3df5cda8f6af77d55dda8ef60c.tar.gz
searxng-577998106aed4c3df5cda8f6af77d55dda8ef60c.zip
[mod] debug mode: use coloredlogs package
in debug mode, add some padding to make the output more readable in procution mode, add the timestamp
Diffstat (limited to 'searx/__init__.py')
-rw-r--r--searx/__init__.py84
1 files changed, 68 insertions, 16 deletions
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)